0

A certain span icon on binary.com has the following html code:

<span id="spot" style="" data-value="3862.76" class="price_moved_down">3,862.76</span>

where the data value changes every 2 second. I want to use that that data value on my web automation script yet I do not know where to start please help. refer to the picture to understand.

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Moshe
  • 107
  • 1
  • 9
  • Selenium is automated, but creates a new browser window. For just getting this value, requests or beautiful soup may be better suited. – Banana Sep 11 '20 at 15:49
  • Does this answer your question? [How to get text with selenium web driver in python](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-web-driver-in-python) – JaSON Sep 11 '20 at 15:51

2 Answers2

1

The number is the text value of the element. So once you find the element using selenium

my_span = driver.find_element_by.....

You can just called the text attribute on the element

print(my_span.text)
fooiey
  • 1,040
  • 10
  • 23
1

To print the text 3,862.76 you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute():

    print(driver.find_element_by_css_selector("span.price_moved_down#spot[data-value]").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//span[@class='price_moved_down' and @id='spot'][@data-value]").text)
    

Ideally, to print the text 3,862.76 you have 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, "span.price_moved_down#spot[data-value]"))).get_attribute("innerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='price_moved_down' and @id='spot'][@data-value]"))).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