title: Webscraping Data from Tradin View using selenium
body: <p>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 -</p>
  <pre><code>exceptions = (NoSuchElementException, StaleElementReferenceException)
  def locate(path, type=&quot;xpath&quot;, time=5):
      global chrome
      global exceptions
      if type == &quot;xpath&quot;:
          element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
              expected_conditions.presence_of_element_located((By.XPATH, path))
          )
      if type == &quot;link_text&quot;:
          element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
              expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
          )
      if type == &quot;name&quot;:
          element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
              expected_conditions.presence_of_element_located((By.NAME, path))
          )
      return element
  </code></pre>
  <p>Here is the full code that I have written:</p>
  <pre><code>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 = &quot;D:\\Repositories\\Bot\\chromedriver v89.0.4389.23.exe&quot;
  exceptions = (NoSuchElementException, StaleElementReferenceException)
  
  
  def locate(path, type=&quot;xpath&quot;, time=5):
      global chrome
      global exceptions
      if type == &quot;xpath&quot;:
          element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
              expected_conditions.presence_of_element_located((By.XPATH, path))
          )
      if type == &quot;link_text&quot;:
          element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
              expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
          )
      if type == &quot;name&quot;:
          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(&quot;https://in.tradingview.com/chart/iVucV9D0/&quot;)
      chrome.maximize_window()
      locate(&quot;Sign in&quot;, &quot;link_text&quot;).click()
      locate(
          &quot;/html/body/div[11]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span&quot;
      ).click()
      locate(&quot;username&quot;, &quot;name&quot;).send_keys(&quot;myemail&quot;)
      locate(&quot;password&quot;, &quot;name&quot;).send_keys(&quot;mypassword&quot; + Keys.ENTER)
      time.sleep(3)
      locate(&quot;/html/body/div[6]/div/div/div[2]/div/div/div[1]/div[2]/form/button&quot;).click()
  
  
  def buy():
      buyprice = locate(
          &quot;/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&quot;
      ).text
      if buyprice != &quot;n/a&quot;:
          return float(buyprice)
      else:
          return &quot;na&quot;
  
  
  def sell():
      sellprice = locate(
          &quot;/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&quot;
      ).text
      if sellprice != &quot;n/a&quot;:
          return float(sellprice)
      else:
          return &quot;na&quot;
  
  
  with webdriver.Chrome(driver) as chrome:
      login()
      while True:
          if buy() != &quot;na&quot;:
              print(&quot;Supertrend Buy detected&quot;)
              # execute rest of the code
          if sell() != &quot;na&quot;:
              print(&quot;Supertrend Sell Detected&quot;)
              # execute rest of the code
  </code></pre>
  <p>Can someone please help me?
  PS: I am using python 3.9 and selenium version 3.141.0</p>