[英]How to extract text from multiple div class using Selenium with Python
I'm trying to get text class price-value from Page_inspect我正在尝试从Page_inspect获取文本 class price-value
Used driver.find_element_by_xpath and WebDriverWait.使用了 driver.find_element_by_xpath 和 WebDriverWait。
rateText=WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//div[starts-with(@class,"price")]//div[contains(@class,"price-value")]')))
for ratevalue in rateText:
print (ratevalue.text)
result not found:未找到结果:
Traceback (most recent call last): File "D:\project\totempop\webscraping\asrPOP.py", line 22, in rateText=WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//div[starts-with(@class,"price")]//div[contains(@class,"price-value")]'))) File "C:\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
追溯(最近调用最后):文件“D:\project\totempop\webscraping\asrPOP.py”,第 22 行,在 rateText=WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, '/ /div[starts-with(@class,"price")]//div[包含(@class,"price-value")]'))) 文件 "C:\Python310\lib\site-packages\selenium\ webdriver\support\wait.py",第 89 行,直到引发 TimeoutException(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:
Thanks in advance提前致谢
Probably, you have the problem with XPath. You should try this one: './/div[contains(@class,"price-value")]/text()'可能你遇到了 XPath 的问题。你应该试试这个:'.//div[contains(@class,"price-value")]/text()'
You can just use this:你可以只使用这个:
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "price-value")))
elements = driver.find_elements(By.CLASS_NAME, "price-value")
for element in elements:
print(element.text)
Seems you were close enough but you need some minor adjustments as follows:看起来你已经足够接近了,但你需要做一些小的调整,如下所示:
The <div class="price">
have a single classname value, so you can drop the contains()
part. <div class="price">
有一个单独的类名值,因此您可以删除contains()
部分。
The <strong class="price-value">
is within the parent <div>
, so you have to adjust the locator strategy . <strong class="price-value">
在父级<div>
中,因此您必须调整定位器策略。
Ideally to get text you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies :理想情况下,要获取文本,您需要为visibility_of_element_located()引入WebDriverWait ,您可以使用以下任一定位器策略:
Using CSS_SELECTOR and text attribute:使用CSS_SELECTOR和文本属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.price div.price-line > strong.price-value"))).text)
Using XPATH and get_attribute("innerHTML")
:使用XPATH和
get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='price']//div[@class='price-line']/strong[@class='price-value']"))).get_attribute("innerHTML"))
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.