admin管理员组文章数量:1122832
i am new to selenium and i have been trying to scrape data from tradingview. i am trying to get the chart data from tradingview chart for btc. i am successful in logging in , but have faced issues trying to locate the X-path to the bitcoin chart. i have tried to inspect element for the class of the chart but was unable to do so. i end up facing this error. please help me with this issue. much appreciated.
erorr message
Failed to extract data: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@class, 'chart-container single-visible top-full-width-chart active')]"}
This is my code.
from selenium import webdriver
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Set up Selenium WebDriver for Colab
options = webdriver.ChromeOptions()
options.add_argument('--headless') # Run Chrome in headless mode
options.add_argument('--no-sandbox') # Needed for Colab
options.add_argument('--disable-dev-shm-usage') # Overcome resource limitations
options.add_argument('--disable-gpu') # Disable GPU for compatibility
options.add_argument('--window-size=1920x1080') # Set a default window size
driver = webdriver.Chrome(options=options)
# Example: Open the TradingView login page
driver.get("/")
# Wait for the login page to load
wait = WebDriverWait(driver, 15)
# newly added by me
email_button = driver.find_element(By.XPATH, "//button[@name='Email']")
email_button.click()
# Login process (replace with your email and password)
# Locate the email/username field using the 'id' or 'name' attribute
email_field = wait.until(EC.presence_of_element_located((By.ID, "id_username")))
email_field.send_keys("tom") # Replace with your email
# Locate the password field using the 'id' or 'name' attribute
password_field = driver.find_element(By.ID, "id_password")
password_field.send_keys("Fs5u+7(3A?") # Replace with your password
# Locate and click the login button
# Edited by me
login_button = driver.find_element(By.XPATH, "//span[text()='Sign in']")
# login_button.click()
# Wait for login to complete (adjust sleep time as necessary)
time.sleep(5)
if "Sign In" in driver.page_source:
print("Login failed. Check your credentials.")
else:
print("Login successful!")
# Navigate to a chart page (e.g., btc chart)
driver.get("/?symbol=INDEX%3ABTCUSD")
time.sleep(5)
# Example: Extract data from a visible container
try:
print('Connected to chart')
data_container = driver.find_element(By.XPATH, "//div[contains(@class, 'chart-container single-visible top-full-width-chart active')]")
print("Extracted Data:")
print(data_container.text)
except Exception as e:
print("Failed to extract data:", e)
# Close the browser
driver.quit()
i am new to selenium and i have been trying to scrape data from tradingview. i am trying to get the chart data from tradingview chart for btc. i am successful in logging in , but have faced issues trying to locate the X-path to the bitcoin chart. i have tried to inspect element for the class of the chart but was unable to do so. i end up facing this error. please help me with this issue. much appreciated.
erorr message
Failed to extract data: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@class, 'chart-container single-visible top-full-width-chart active')]"}
This is my code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Set up Selenium WebDriver for Colab
options = webdriver.ChromeOptions()
options.add_argument('--headless') # Run Chrome in headless mode
options.add_argument('--no-sandbox') # Needed for Colab
options.add_argument('--disable-dev-shm-usage') # Overcome resource limitations
options.add_argument('--disable-gpu') # Disable GPU for compatibility
options.add_argument('--window-size=1920x1080') # Set a default window size
driver = webdriver.Chrome(options=options)
# Example: Open the TradingView login page
driver.get("https://www.tradingview.com/accounts/signin/")
# Wait for the login page to load
wait = WebDriverWait(driver, 15)
# newly added by me
email_button = driver.find_element(By.XPATH, "//button[@name='Email']")
email_button.click()
# Login process (replace with your email and password)
# Locate the email/username field using the 'id' or 'name' attribute
email_field = wait.until(EC.presence_of_element_located((By.ID, "id_username")))
email_field.send_keys("tom") # Replace with your email
# Locate the password field using the 'id' or 'name' attribute
password_field = driver.find_element(By.ID, "id_password")
password_field.send_keys("Fs5u+7(3A?") # Replace with your password
# Locate and click the login button
# Edited by me
login_button = driver.find_element(By.XPATH, "//span[text()='Sign in']")
# login_button.click()
# Wait for login to complete (adjust sleep time as necessary)
time.sleep(5)
if "Sign In" in driver.page_source:
print("Login failed. Check your credentials.")
else:
print("Login successful!")
# Navigate to a chart page (e.g., btc chart)
driver.get("https://www.tradingview.com/chart/kvfFlBvq/?symbol=INDEX%3ABTCUSD")
time.sleep(5)
# Example: Extract data from a visible container
try:
print('Connected to chart')
data_container = driver.find_element(By.XPATH, "//div[contains(@class, 'chart-container single-visible top-full-width-chart active')]")
print("Extracted Data:")
print(data_container.text)
except Exception as e:
print("Failed to extract data:", e)
# Close the browser
driver.quit()
Share
Improve this question
asked yesterday
lm10lm10
94 bronze badges
1
- Login user name or pass is wrong. – Subir Chowdhury Commented yesterday
1 Answer
Reset to default 0Since I don't want to create an account I tried it without logging in.
With the URL: https://www.tradingview.com/chart/?symbol=INDEX%3ABTCUSD
It looks like the element doesn't load before you try to access it.
You have already used explicit wait in your code. You should also try it for the data_container
element instead of manual sleep.
Try the following :
# Navigate to a chart page (e.g., btc chart)
driver.get("https://www.tradingview.com/chart/?symbol=INDEX%3ABTCUSD")
# Example: Extract data from a visible container
try:
print('Connected to chart')
data_container = wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'chart-container single-visible top-full-width-chart active')]")))
print("Extracted Data:")
print(data_container.text)
except Exception as e:
print("Failed to extract data:", e)
本文标签: python selenium trying to get data from tradingview chartStack Overflow
版权声明:本文标题:python selenium trying to get data from tradingview chart - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280920a1926154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论