Python 使用selenium从Tradin视图中删除数据
我需要使用无限循环从交易视图图表中获取数据,但我一直遇到这个错误-StaleElementReferenceException。 我已尝试使用以下函数使程序显式等待-Python 使用selenium从Tradin视图中删除数据,python,selenium,algorithmic-trading,tradingview-api,Python,Selenium,Algorithmic Trading,Tradingview Api,我需要使用无限循环从交易视图图表中获取数据,但我一直遇到这个错误-StaleElementReferenceException。 我已尝试使用以下函数使程序显式等待- exceptions = (NoSuchElementException, StaleElementReferenceException) def locate(path, type="xpath", time=5): global chrome global exceptions if
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:我正在使用Python3.9和selenium版本3.141.0,我发现许多这些奇怪的问题可以通过简单地降级到旧的chromedriver和/或chrome/chrome,或者切换到firefox/geckodriver来解决。Selenium、Web驱动程序和浏览器之间的兼容性范围非常狭窄,令人难以忍受。您是否考虑过改用scrapy()?Selenium真的应该用于测试。我是一名新的程序员,我尝试使用scrapy,但无法理解文档。你能告诉我使用哪个版本的chrome驱动程序吗?如果你搜索“Selenium webdriver compatibility matrix”,你会发现很多资源,很多都在这里。不幸的是,我现在没有时间深入研究我的代码。我只是有机会重温一些代码。工作原理:selenium 3.14.1、firefox-esr_60.9.0、geckodriver-v0.27.0。我在chrom{e,ium}方面有太多问题,在很多项目之前就停止使用它了。感谢man@jcomeau_ictx帮助了很多人。请“接受”答案好吗?谢谢,jc