0

basically I try to write a code that automatically clicks the cookie and, every 3 seconds, buys an item of the found elements without the class-attribute of "grayed" (as those are the ones u dont have enough cookies to buy for) Angela Yu made it pretty complicated with checking cookies vs a dict consisting of nested lists with "id" and "price" etc. but I thought I could make this shorter... any help? code works fine until first item is bought, then I get the "stale element reference: stale element not found in the current frame" error

heres the code:

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")

big_cookie = driver.find_element(By.ID, "cookie")
store = driver.find_elements(By.CSS_SELECTOR, "div #store div")
item_ids = [item.get_attribute("id") for item in store]

timeout = time.time() + 3

while True:
    big_cookie.click()
    if time.time() > timeout:
        for element in store:
            if element.get_attribute("class") == "grayed":
                pass
            else:
                element.click()
        timeout = time.time() + 5

1 Answer 1

1

Fixed it by adding try... except with StaleElementReferenceException... probably sloppy I know and Im missing the functionality of not instantly buying all cursors before I can buy a grandma or factory but thats okay... heres the code:

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

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")
big_cookie = driver.find_element(By.ID, "cookie")
wait = WebDriverWait(driver, 5)
while True:
    big_cookie.click()
    store_items = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div#store > div")))
    try:
        for item in store_items:
            if item.get_attribute("class") == "grayed":
                pass
            else:
                item.click()
    except StaleElementReferenceException:
        store_items = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div#store > div")))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.