0

With Python and Selenium, I try to extract a text value from a website. Extract of HTML :

<div order=2" class="css-1asuq02 e1y0u4b10">
<p>
<span role="img" aria-label="icon-circle" class="css-shla5t ef02kcv0">
</span>
"343 W"
</p>
</div>

Python code:

driver.find_element(By.XPATH, value= ???)

I tried :

driver.find_elements(By.XPATH, value='//div[@order="2"]')

It works but too many elements

How to select ONLY this order="2" and class (Xpath, contains, other) ?

I want to retrieve 343 value

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To print the text 343 W you can use either of the following locator strategies:

  • Using css_selector`:

    print(driver.find_element(By.CSS_SELECTOR, "div[order='2'] > p").text)
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//div[@order='2']/p").text)
    

To extract the text 343 W ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[order='2'] > p"))).text)
    
  • Using XPATH`:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@order='2']/p"))).text)
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352