0

I am working on a Game Playing Bot for Cookie Clicker game. The bot clicks the cookie and buys the items automatically. You would understand what the bot does once you try the game yourself. Link: https://orteil.dashnet.org/experiments/cookie/

Problem: Keep getting this exception within seconds of its execution:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found
(Session info: chrome=114.0.5735.198)

Now I tried executing this multiple times and it crashed at different intervals of time. I even recoded the entire project after multiple executions. From what I understood online, this happens when there's an update in the browser or any user interation during the execution. But I did not touch my keyboard during the execution. Here's the code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get(url="http://orteil.dashnet.org/experiments/cookie/")
clicker = driver.find_element(By.XPATH, '//*[@id="cookie"]')
while True:
    clicker.click()
    money = int(driver.find_element(By.XPATH, '//*[@id="money"]').text)
    cursor_price = int(driver.find_element(By.XPATH, '//*[@id="buyCursor"]/b').text.split()[-1])
    if money > cursor_price:
        driver.find_element(By.XPATH, '//*[@id="buyCursor"]/b').click()

Should I try using a different browser?

  • it's clearly visible that you're having user interaction when you use `click()` which changes /updates the browser – Ajeet Verma Jul 14 '23 at 04:13

2 Answers2

1
StaleElement Exception occurs when an element is stale from the DOM sheet, and it probably happens when the page gets refreshed, below code helped me when I am in the same situation.

Webelement ele=driver.findelement(By.xpath(""));
wait.until(ExpectedCondition.Invisiblityofelemntlocated(ele));
Bhairu
  • 72
  • 4
0

StaleElementReferenceException

StaleElementReferenceException is thrown when a reference to an element is no longer appears on the DOM of the page and is stale.

Some of the possible causes of StaleElementReferenceException can be:

  • You are no longer on the same page, or the page may have refreshed since the element was located.
  • The element may have been removed and then re-added again to the screen, since it was initially located.
  • Element may have been inside an iframe or another context which was refreshed.

This usecase

In the Cookie Clicker game to continious click on the cookie till the count is 15 and then to click on the Cursor - 15 you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

driver.get("https://orteil.dashnet.org/experiments/cookie/")
while True:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='cookie']"))).click()
    money = int(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='money']"))).text)
    cursor_price = int(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='buyCursor']/b"))).text.split()[-1])
    if money > cursor_price:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='buyCursor']/b"))).click()
        break
        

Browser snapshot:

Cookie Clicker

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352