python - Selenium and Cookie Clicker Game - "stale element not found in the current frame" - Stack Overflow

basically I try to write a code that automatically clicks the cookie and, every 3 seconds, buys an item

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.webdrivermon.by import By
import time

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("/")

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

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.webdrivermon.by import By
import time

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=options)
driver.get("https://orteil.dashnet./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
Share Improve this question asked Jan 29 at 16:38 kutari studioskutari studios 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 seleniummon.exceptions import StaleElementReferenceException
from selenium.webdriver.chrome.options import Options
from selenium.webdrivermon.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./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")))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289743a4620783.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信