3

So I have this code which is referencing this html class:

<div class="text-right _2jRRJJvarKXJGP9oRP-Bv0 pbBzNArud8-t5yrNZi8Wg rankings-list-volume">2775.60</div> 
Vol = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div/main/div/div/div[2]/a[1]/div[8]').text
print(Vol)

If I run it without the .text it returns just the element name and such so I know it is finding the element but it won't return the text. The 2775.60 value within the HTML is what it should be returning but instead, it just prints nothing.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
mbohde
  • 109
  • 1
  • 7

1 Answers1

2

To print the text 2769.94 you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute():

    print(driver.find_element_by_css_selector("div.rankings-list-volume").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//div[contains(@class, 'rankings-list-volume')]").text)
    

Ideally, to print the text 2769.94 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.rankings-list-volume"))).get_attribute("innerHTML"))
    
  • Using XPATH and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'rankings-list-volume')]"))).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


Outro

Link to useful documentation:

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