简体   繁体   中英

How to extract text from multiple div class using Selenium with Python

I'm trying to get text class price-value from Page_inspect

Used driver.find_element_by_xpath and 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:

Thanks in advance

Probably, you have the problem with XPath. You should try this one: './/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.

  • The <strong class="price-value"> is within the parent <div> , so you have to adjust the locator strategy .

  • 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 :

    • Using CSS_SELECTOR and text attribute:

       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") :

       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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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