0

I'm learning Selenium, and I've tried to make an automatic clicker for this game https://orteil.dashnet.org/cookieclicker/ .

I've made a Action Chain to click on the big cookie on the left side and put this into the loop. But it click only once.

I tried, this loop also on https://clickspeedtest.com/ page, with same reasoult. I also tried to add actions.pause(1), and time.sleep(1) inside the loop.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time


PATH = r"C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://orteil.dashnet.org/cookieclicker/")

driver.implicitly_wait(5)

bigCookie = driver.find_element_by_id("bigCookie")

actions = ActionChains(driver)
actions.click(bigCookie)

for i in range(10):
    actions.perform()
polipopik
  • 73
  • 4
  • the large cookie thing has to be located every time before you click it – PSR Feb 01 '22 at 18:51
  • Does this answer your question? [Action Chain in loop works only once(Selenium/Python)](https://stackoverflow.com/questions/70279480/action-chain-in-loop-works-only-onceselenium-python) – PSR Feb 01 '22 at 18:52
  • Yes, thank you :) – polipopik Feb 01 '22 at 20:23

2 Answers2

2

When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.

I assume that after the first time you run perform(), the queue stays empty and you probably need to store new set of actions in the queue. So something like this:

actions = ActionChains(driver)
    
for i in range(10):
    actions.click(bigCookie)
    actions.perform()
Eugene S
  • 6,709
  • 8
  • 57
  • 91
0

ActionChains are used in a chain pattern. In other words, actions can be queued up one by one, then performed. When you call perform(), the events are fired in the order they are queued up.

You were almost there. However to perform the clicks in a loop, you need to create the ActionChain including both the events as follows:

driver.get('https://orteil.dashnet.org/cookieclicker/')
for i in range(10):
    ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR, "div#bigCookie")).click().perform()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352