简体   繁体   English

如何使用 Selenium 获取 find_elements(不是 find_element)的 innerHTML

[英]How to get innerHTML of find_elements (not find_element) with Selenium

My goal is to get this price text (2078--as shown in the pic), it works with find_element but the values in the output would be the same across the loop.我的目标是获取此价格文本(2078——如图所示),它与 find_element 一起使用,但 output 中的值在整个循环中都是相同的。

在此处输入图像描述 在此处输入图像描述

heres my code:这是我的代码:

#Extracting the information from the results #从结果中提取信息
for entry in entries: #Empty list labels=[] #Extracting the Name, adress, Phone, and website: for entry in entries: #Empty list labels=[] #Extracting the Name, address, Phone, 和网站:

name= entry.get_attribute("aria-label")
adress = entry.find_element(By.XPATH, '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[3]/div[1]/div[5]/div/div[2]/div[2]/div[3]/div/div[2]/div/div/jsl[1]/div[2]')
adress_get = adress.get_attribute('innerHTML')


phone = entry.find_elements(By.CLASS_NAME, 'xwpmRb.qisNDe').text
try:
    webcontainer= entry.find_elements(By.CLASS_NAME, 'W4Efsd')
    website="Hello"
except NoSuchElementException:
    website="No website could be found"
print (name)
print (adress_get)
print (phone)
print (website)

My goal is to get list prices text from google map page (Web Scraping).我的目标是从谷歌 map 页面(Web Scraping)获取标价文本。

To print all the texts of all the elements found by find_elements you need to do the following:要打印find_elements找到的所有元素的所有文本,您需要执行以下操作:

phones = entry.find_elements(By.CLASS_NAME, 'xwpmRb.qisNDe')
for phone in phones:
    print(phone.text)

Or you can create a variable that holds elements' texts using this list comprehension:或者您可以使用此列表理解创建一个变量来保存元素的文本:

phones = entry.find_elements(By.CLASS_NAME, 'xwpmRb.qisNDe')
phones_texts = [phone.text for phone in phones]

And then you can do whith it what you want, eg print the list or each element:然后你可以做你想做的事,例如打印列表或每个元素:

print(phones_texts)
# OR
for text in phones_texts:
    print(text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM