最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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.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")))
发布评论

评论列表(0)

  1. 暂无评论