Webdriver selenium无法从TradingView中找到任何元素

2024-05-08 14:54:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对使用Selenium还不熟悉,但我看了足够多的视频,关注了足够多的文章,知道缺少了一些东西。我试图从TradingView中获取值,但我遇到的问题是,我根本找不到任何元素,不是通过Xpath或Css。我继续做了一个简单的可见性元素测试,如下面的代码所示,让我惊讶的是它超时了

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# Stops the UI interface (chrome browser) from popping up
# chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path='c:\se\chromedriver.exe', options=chrome_options)

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time

driver.get("https://www.tradingview.com/chart/")

timeout = 20
try:
    WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]")))
    print("Page loaded")
except TimeoutException:
    print("Timed out waiting for page to load")
driver.quit()

我也试着用下面的方法点击其中一个图表按钮,但也不起作用。我注意到,与许多其他Tradingview网站不同,元素没有名称,也没有使用Xpath生成相对路径(仅完整路径)

driver.find.element_by_xpath('/html/body/div[2]/div[5]/div/div[2]/div/div/div/div/div[4]').click()

非常感谢您的帮助


Tags: fromimportdiv元素supportbydriverselenium
1条回答
网友
1楼 · 发布于 2024-05-08 14:54:12

我认为xpath一定有问题。 当我尝试单击AAPL按钮时,它对我有效。 我使用的xpath是:

(//div[contains(text(),'AAPL')])[1]

如果您指定要单击的元素,我将尝试。 也要熟悉框架的概念,因为这类网站有很多框架

相关问题 更多 >