使用 selenium 从 Tradingview 抓取数据

Webscraping Data from Tradin View using selenium

我需要使用无限循环从交易视图图表中抓取数据,但我将 运行 保留在这个错误中 - StaleElementReferenceException。 我尝试使用以下函数让程序显式等待 -

exceptions = (NoSuchElementException, StaleElementReferenceException)
def locate(path, type="xpath", time=5):
    global chrome
    global exceptions
    if type == "xpath":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.XPATH, path))
        )
    if type == "link_text":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
        )
    if type == "name":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.NAME, path))
        )
    return element

这是我编写的完整代码:

from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = "D:\Repositories\Bot\chromedriver v89.0.4389.23.exe"
exceptions = (NoSuchElementException, StaleElementReferenceException)


def locate(path, type="xpath", time=5):
    global chrome
    global exceptions
    if type == "xpath":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.XPATH, path))
        )
    if type == "link_text":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
        )
    if type == "name":
        element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
            expected_conditions.presence_of_element_located((By.NAME, path))
        )
    return element


def login():
    global chrome
    chrome.get("https://in.tradingview.com/chart/iVucV9D0/")
    chrome.maximize_window()
    locate("Sign in", "link_text").click()
    locate(
        "/html/body/div[11]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span"
    ).click()
    locate("username", "name").send_keys("myemail")
    locate("password", "name").send_keys("mypassword" + Keys.ENTER)
    time.sleep(3)
    locate("/html/body/div[6]/div/div/div[2]/div/div/div[1]/div[2]/form/button").click()


def buy():
    buyprice = locate(
        "/html/body/div[2]/div[5]/div/div[1]/div[1]/div[5]/div/div[2]/div[1]/div[3]/div[2]/div[3]/div[2]/span"
    ).text
    if buyprice != "n/a":
        return float(buyprice)
    else:
        return "na"


def sell():
    sellprice = locate(
        "/html/body/div[2]/div[5]/div/div[1]/div[1]/div[5]/div/div[2]/div[1]/div[3]/div[2]/div[6]/div[2]/span"
    ).text
    if sellprice != "n/a":
        return float(sellprice)
    else:
        return "na"


with webdriver.Chrome(driver) as chrome:
    login()
    while True:
        if buy() != "na":
            print("Supertrend Buy detected")
            # execute rest of the code
        if sell() != "na":
            print("Supertrend Sell Detected")
            # execute rest of the code

有人可以帮助我吗? PS:我正在使用 python 3.9 和 selenium 版本 3.141.0

我发现很多奇怪的问题都可以简单地通过降级到旧的 chromedriver and/or chrome/chromium 或切换到 firefox/geckodriver 来解决。 Selenium、网络驱动程序和浏览器之间的兼容性范围非常狭窄且无情。