captainsameer
captainsameer

Reputation: 55

StaleElementReferenceException in python selenium

I am trying to count how many time "Load More Reviews" option is clicked from this site. But I am getting the following error:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Here is my python code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
url = "https://www.justdial.com/Delhi/S-K-Premium-Par-Hari-Nagar/011PXX11-XX11-131128122154-B8G6_BZDET"
driver.get(url)
pop_up = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="best_deal_detail_div"]/section/span')))
pop_up.click() # For disable pop-up
count = 0
while True:
  element = WebDriverWait(driver, 20).until(
  EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']")))
  element.click()
  count = count + 1
  print(count)

Upvotes: 1

Views: 76

Answers (1)

rahul rai
rahul rai

Reputation: 2326

Try below code:

  count = 0
while True:
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']"))).click()
        count = count + 1
    except StaleElementReferenceException:
        pass
    except TimeoutException:
        break
print(count)

Issue: As per your code you are waiting for Load More Reviews button to be clickable, now once its clicked and even before page has finished loading its detecting if button is there and clickable, but when its try to click , by that time page is still in process of refresing / loading more reviews. As a result HTML DOM is disrupted/refreshed and stale element exception comes.

Also as there is no break condition in your code, i have added one. If there is no Load More Reviews button on page. It will break out of loop.

Upvotes: 1

Related Questions