python selenium 尝试从交易视图图表中获取数据

问题描述 投票:0回答:1

我是硒的新手,我一直在尝试从交易视图中抓取数据。我正在尝试从 BTC 的交易视图图表中获取图表数据。我成功登录,但在尝试定位比特币图表的 X 路径时遇到了问题。我试图检查图表类的元素,但无法这样做。我最终面临这个错误。请帮我解决这个问题。非常感谢。

错误消息

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')]"}

这是我的代码。

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()
python selenium-webdriver tradingview-api
1个回答
0
投票

由于我不想创建帐户,所以没有登录就尝试了。 网址为:

https://www.tradingview.com/chart/?symbol=INDEX%3ABTCUSD

在您尝试访问该元素之前,该元素似乎尚未加载。

您已经在代码中使用了显式等待。您还应该尝试使用

data_container
元素而不是手动睡眠。

尝试以下方法:

# 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)
© www.soinside.com 2019 - 2024. All rights reserved.