I need to web scrape data from a trading view chart using an infinite loop, but I keep running into this error - StaleElementReferenceException. I have tried making the program wait explicitly using the following function -
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
Here is the full code that I have written:
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
Can someone please help me? PS: I am using python 3.9 and selenium version 3.141.0
I've found that a lot of these weird issues can be solved simply by downgrading to an older chromedriver and/or chrome/chromium, or switching to firefox/geckodriver. The range of compatibility between Selenium, the webdriver, and the browser is very narrow and unforgiving.