Automating cookie clicker with python and selenium

Aashrut Agarwal
4 min readJan 11, 2021

Python is a programming language with many different packages and modules. These packages can do everything from machine learning to data science to playing chess. One very good package for web scraping and automation is selenium.

Today we are going to write a simple chunk of python code with selenium to automate cookie clicker. Cookie clicker is a very simple game where you click a cookie to get cookies. You can buy products that click for you and upgrades that improve cps(cookies per second).

We start by importing all the libraries needed.

import time
from selenium import webdriver
from selenium.webdriver import *
import keyboard

We are importing the part of selenium that we need , the time module and the keyboard module.

You are going to need to download the selenium webdriver for whichever browser you are using. I am using google chrome and have downloaded the chrome webdriver.

You can find the different webdrivers here: https://selenium-python.readthedocs.io/installation.html . This website has all the commands for selenium and is the documentation. If you get stuck at any time refer to the docs for help.

Lets get started! We first have to add options. These are the settings for when we open google. I am going to add the option to stay maximized. This is so that google is fully maximized when we run the code.

options = Options()
options.add_argument("--start-maximized")

We have defined the options for chrome in a variable called options. Now we can get chrome up and running.

driver = webdriver.Chrome(chrome_options = options, executable_path = r"Enter the path to your driver here")

The r before the path is so that python reads the path as raw text. As you can see this is very chrome specific. Let’s open up the website.

driver.get("https://orteil.dashnet.org/cookieclicker/")

This will open up the website for cookie clicker. If you want to open a different website just put a different url in the Quotes.

This is what we have so far:

import time
from selenium import webdriver
from selenium.webdriver import *
import keyboard
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options = options, executable_path = r"Enter the path to your driver here")driver.get("https://orteil.dashnet.org/cookieclicker/")

Now we start coding the logic for playing the game.

We should split the total project into tasks that we need to do:

  1. Click on the cookie repeatedly
  2. Buy any products or upgrades that we can

We should look for the cookie in the html to find the defining features

The html for the game

We can see that the id for the big cookie is “bigCookie”. Now, let us define a function to find the cookie and click on it.

def Click_on_cookie(num):    
Big_cookie = driver.find_element_by_id("bigCookie")
for i in range(num):
Big_cookie.click()

This code will find an element with the id “bigCookie” and then click on it any number of times.

If we want to test this, we can whip up some quick code, with a kill switch. We will make use of the keyboard module for the kill switch.

import time
from selenium import webdriver
from selenium.webdriver import *
import keyboard
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options = options, executable_path = r"Enter the path to your driver here")driver.get("https://orteil.dashnet.org/cookieclicker/")def Click_on_cookie(num):
Big_cookie = driver.find_element_by_id("bigCookie")
for i in range(num):
Big_cookie.click()
#time to wait for the site to load
time.sleep(10)
while true:
Click_on_cookie(1)
#Kill switch
if keyboard.is_pressed("q"):
break

When this code runs, it will repeatedly press the cookie. It can be stopped by holding down “q”.

We have completed step 1.

Now we should work on buying the products and upgrades. If we go to the html we can find that the id changes based on our cookie score. If we have unlocked a product, the class name has “unlocked”. This seems like the solution.

This will not work because we have to unlock the item but also have enough cookies to pay for it. While this seems like a daunting task. A tiny bit more digging through the html tells us that if we can buy an item i.e. have enough cookies to purchase it, the class name changes to contain “enabled”.

If we run some code to get a list of all the items with enabled in the class name and then click on them, we can click on every product and upgrade.

#Game Loop
while True:
Click_on_cookie(5)
#Buy all the items
try:
items = driver.find_elements_by_class_name("enabled")
for item in items[::-1]:
item.click()
except:
print("Not enough cookies")
#Kill switch
if keyboard.is_pressed("q"):

We define items as a list containing every enabled item. We then loop through the list items, backwards(to buy the best products first) and click on the item. It is written in a try keyword in case of an error. An error will arise if there are no elements that are “enabled”.

The except keyword will print “Not enough cookies” if there is ever an error i.e. no enabled elements.

If we combine this with the first chunk of code we will get a fully working cookie clicker bot in python selenium

import time
from selenium import webdriver
from selenium.webdriver import *
import keyboard
options = Options()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options = options, executable_path = r"Enter the path to your driver here")driver.get("https://orteil.dashnet.org/cookieclicker/")def Click_on_cookie(num):
Big_cookie = driver.find_element_by_id("bigCookie")
for i in range(num):
Big_cookie.click()
#time to wait for the site to load
time.sleep(10)
#Game Loop
while True:
Click_on_cookie(5)
#Buy all the items
try:
items = driver.find_elements_by_class_name("enabled")
for item in items[::-1]:
item.click()
except:
print("Not enough cookies")
#Kill switch
if keyboard.is_pressed("q"):

Selenium is a very powerful tool and combined with the very versatile language python has endless possibilities. These 27 lines of code only scratch the surface of what is possible with selenium.

--

--