andreg
andreg

Reputation: 11

How to retrieve the value in a class using selenium and Python

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

Upvotes: 0

Views: 128

Answers (2)

vitaliis
vitaliis

Reputation: 4212

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.

Upvotes: 0

Vova
Vova

Reputation: 3541

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

Upvotes: 1

Related Questions