0

I'm trying to get a number(the value of bitcoin) from a site, using this code

from selenium import webdriver

driver = webdriver.Firefox()

driver.get('https://it.tradingview.com/symbols/BTCEUR/?exchange=BINANCE')
el = driver.find_element_by_class_name('tabValue-HYHP1WHx')
el_value = el.get_attribute("value")
print(el)
driver.quit()

It should print a number, but it prints None. I tryed to use other modules such as .getText, but the result is always None. What can I use to get the value I want?

I want one between the red one and the orange one: enter image description here

4
  • You have to wait until the element is loaded in page before getting its attribute. That's probably why it returns None Commented Mar 25, 2021 at 15:04
  • There are 16 elements on the page that match your locator. You need to describe which element on the page you are trying to get the value from. A screenshot of the page indicating which one would be extra helpful. Commented Mar 25, 2021 at 16:28
  • I added the picture Commented Mar 26, 2021 at 10:03
  • I added locator for the red one. Thanks for the picture. Previous locator was for the orange one. Commented Mar 26, 2021 at 12:04

2 Answers 2

1

I've found there already a solution, but might that will help as well:

import time

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('https://it.tradingview.com/symbols/BTCEUR/?exchange=BINANCE')
time.sleep(3)
el = driver.find_element_by_xpath("//div[@class='tv-symbol-price-quote__value js-symbol-last']")
el_value = el.text
print(el_value)
driver.quit()

by webdriver type, use you preferable one

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

Comments

0

Your locator is not unique. Try to use mine. Also you are printing element, not its value.

driver.get('https://it.tradingview.com/symbols/BTCEUR/?exchange=BINANCE')
el = driver.find_element_by_css_selector('.tv-symbol-price-quote__value.js-symbol-last')
el_value = el.get_attribute("value")
print(el_value)
driver.quit()

If el_value = el.get_attribute("value") won't work, use

el_value = el.text

To wait for your element use:

from selenium.webdriver.support.wait import WebDriverWait

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((SelectBy.CSS_SELECTOR, ".tv-symbol-price-quote__value.js-symbol-last")))

10 - number of seconds you wait.

7 Comments

Con questo ha funzionato, grazie mille
When I copy the CSS selector of a web object, for example the one in the question, it gives me something different from yours, and doesn't work. How do I get the correct one?
@vitaliis like css selectors, but prefer xpath more, easier and fleaxible
I updated locator. It should get that fields value.
@vitaliis so do I, even better to use just ids, and sometimes css selector more shorter
|

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.