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

Time:04-22

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

CodePudding user response:

Probably, you have the problem with XPath. You should try this one: './/div[contains(@class,"price-value")]/text()'

CodePudding user response:

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)

CodePudding user response:

Seems you were close enough but you need some minor adjustments as follows:

  • The <div > have a single classname value, so you can drop the contains() part.

  • The <strong > 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
    
  • Related