如何用Selenium+Python实现Cookie Clicker自动点击及报错排查
Let's break down why your code is throwing that AttributeError and get your bot clicking cookies properly!
The Root Cause of the Error
Your main issue is using find_elements_by_id() instead of find_element_by_id():
find_elements_by_id()returns a list of WebElements (even if only one element matches the ID)ActionChains.click()expects a single WebElement, not a list — that's exactly why you're seeing the "move_to requires a WebElement" error
There are a few other small tweaks we should make to ensure the bot runs smoothly too.
Corrected Code
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Fix the path: use raw string to avoid escape character issues PATH = r'C:\Program Files (x86)\chromedriver.exe' driver = webdriver.Chrome(PATH) driver.get('https://orteil.dashnet.org/cookieclicker/') # Use explicit wait instead of implicit wait for more reliability # Wait for the big cookie to be clickable cookie = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'bigCookie')) ) # Optional: Wait for cookie count element to load if you need it later cookie_count = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'cookies')) ) # Fix items list: use single element lookup and wrap in wait items = [WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, f'productPrice{i}')) ) for i in range(1, -1, -1)] actions = ActionChains(driver) # Click the single cookie element, not a list actions.click(cookie) # Perform the click repeatedly for _ in range(5000): actions.perform() # Optional: Close the browser when done # driver.quit()
Key Changes Explained
Switched to single element lookups:
- Replaced
find_elements_by_id()with explicit waits targeting single elements. This ensures we pass a valid WebElement toActionChains.click()instead of a list. Explicit waits are more reliable than implicit waits here — they wait until elements are actually ready to interact with, avoiding race conditions on dynamic pages like Cookie Clicker.
- Replaced
Fixed the file path:
- Added an
rbefore the path string to make it a raw string, which prevents Python from interpreting backslashes as escape characters (like\xin your original path).
- Added an
Cleaned up item list creation:
- Used f-strings for cleaner ID formatting, and wrapped each product price lookup in a wait to ensure those elements load properly before we reference them.
Quick Side Note
The Bluetooth error in your terminal is a harmless ChromeDriver bug — it doesn't affect your bot's functionality, so you can safely ignore it. If you want the bot to run indefinitely instead of just 5000 clicks, replace the for loop with a while True: loop (just remember to press Ctrl+C to stop it).
内容的提问来源于stack exchange,提问作者Noah Sullivan

