Home > OS >  Action Chain in loop works only once(Selenium/Python)
Action Chain in loop works only once(Selenium/Python)

Time:12-09

I'm trying to implement a cookie clicker bot. Cookie clicker it's just a stupid simple game, where you can click on the cookie to earn more cookies. You can take a look at that masterpiece errors

CodePudding user response:

What you trying to do here is to load the gun one time and then to press on trigger several times in a loop... To do what you wish you should slightly change your code as following:

actions = ActionChains(driver)

for i in range(4000):
    actions.click(cookie)
    actions.perform()

BTW I guess this code will still not work since after the first click on the cookie element even if it will appear again it will be a NEW, ANOTHER element even if it could be located with the same locator.
So trying to click it again will cause StaleElementReferenceException.
To make this work you will have to locate the cookie element again each time, as following:

actions = ActionChains(driver)

for i in range(4000):
    cookie = wait.until(EC.visibility_of_element_located((By.ID, "bigCookie")))
    actions.click(cookie)
    actions.perform()
  • Related