Home > database >  Selenium loop stops with StaleElementReferenceException
Selenium loop stops with StaleElementReferenceException

Time:02-17

I am doing day 48 from 100 Days of Code: The Complete Python Pro Bootcamp for 2022. It is based on this clicking game: http://orteil.dashnet.org/experiments/cookie/ Problem I have here is the loop clicks tier_2 just once and then I get error:

Traceback (most recent call last): File "C:/Users/Mariusz/Desktop/100-Days-of-Code/Intermediate/Day 48 - Selenium/main.py", line 44, in tier_2.click() File "C:\Users\Mariusz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "C:\Users\Mariusz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params) File "C:\Users\Mariusz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\Mariusz\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=98.0.4758.102)

Code is below. What am I doing wrong? I've been sitting at it for good 2 hours, been googling, tried break and continue at different points but result is same. Will appreciate any help

import selenium.webdriver.common.keys
import time

path = r"C:\Users\Mariusz\Desktop\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=path)

driver.get("http://orteil.dashnet.org/experiments/cookie/")

cookie = driver.find_element_by_id("cookie")
tier_8 = driver.find_element_by_id("buyTime machine")
tier_7 = driver.find_element_by_id("buyPortal")
tier_6 = driver.find_element_by_id("buyAlchemy lab")
tier_5 = driver.find_element_by_id("buyShipment")
tier_4 = driver.find_element_by_id("buyMine")
tier_3 = driver.find_element_by_id("buyFactory")
tier_2 = driver.find_element_by_id("buyGrandma")
tier_1 = driver.find_element_by_id("buyCursor")


check = time.time()   5
timeout = time.time()   300

while time.time()<timeout:
    cookie.click()
    money_web = driver.find_element_by_id("money")
    money = int(money_web.text)
    if time.time() > check:
        if money >= 123456789:
            tier_8.click()
        elif money >= 1000000:
            tier_7.click()
        elif money >= 50000:
            tier_6.click()
        elif money >= 7000:
            tier_5.click()
        elif money >= 2000:
            tier_4.click()
        elif money >= 500:
            tier_3.click()
        elif money >= 123:
            tier_2.click()
        elif money >= 17:
            tier_1.click()
        check = time.time()   5


CodePudding user response:

StaleElementReferenceException means that the element is no longer part of the DOM, or that it was moved to another span or div or any other element.

Add Try/Catch block the your code. When you catch the error - find the element again.

money_web = driver.find_element_by_id("money")
try {
    doStufOnElement(money_web)
    }
catch (exception) {
    money_web = driver.find_element_by_id("money");
    doStufOnElement(money_web)
}

CodePudding user response:

I found solution. Problem was that this bit and all similar:

tier_2 = driver.find_element_by_id("buyGrandma")

was outside of loop and because it is find element, not elements it was called only once. I took it and all similar into loop like that:

elif money >= grandma_price:
tier_2 = driver.find_element_by_id("buyGrandma")
tier_2.click()

and now it does not stop and carries on and clicks the same element many times.

  • Related