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

2
  • Selenium is automated, but creates a new browser window. For just getting this value, requests or beautiful soup may be better suited. Commented Sep 11, 2020 at 15:49
  • Does this answer your question? How to get text with selenium web driver in python Commented Sep 11, 2020 at 15:51

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.