【问题标题】:Webscraping Data from Tradin View using selenium使用 selenium 从 Tradingview 抓取数据
【发布时间】:2021-04-19 14:41:37
【问题描述】:

我需要使用无限循环从交易视图图表中抓取数据,但我一直遇到此错误 - 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

【问题讨论】:

  • 您是否考虑过使用 scrapy (scrapy.org) 代替? Selenium 确实应该用于测试。
  • 我是一个新的编码员,我尝试使用scrapy但无法弄清楚文档。

标签: python selenium algorithmic-trading tradingview-api


【解决方案1】:

我发现很多这些奇怪的问题都可以通过降级到较旧的 chromedriver 和/或 chrome/chromium,或切换到 firefox/geckodriver 来解决。 Selenium、webdriver 和浏览器之间的兼容性范围非常狭窄且无情。

【讨论】:

  • 你能指导我使用哪个版本的 chrome 驱动程序吗?
  • 如果你用谷歌搜索“selenium webdriver 兼容性矩阵”,你会发现很多资源,其中很多都在 SO 上。不幸的是,我现在没有时间深入研究我的代码。
  • 刚刚有机会重温一些代码。什么有效:selenium 3.14.1、firefox-esr_60.9.0、geckodriver-v0.27.0。我在使用 chrom{e,ium} 时遇到了太多问题,在许多项目之前就停止在 selenium 中使用它了。
  • 您能“接受”这个答案吗?谢谢——jc
猜你喜欢
  • 2023-01-26
  • 2021-10-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多