Skip to content
Program to Click a Button N times

Program to Click a Button N times

When I started to learn and experiment with code, one day I just thought what if we can automate the way we browse the internet like some code to automatically sign in and follow, like or click a few buttons for us and perform all of this repeatedly and then I ran into Selenium and it’s just Awesome, Fantastic I mean you should give it a try and you’ll love it.

Requirements

  • Selenium [pip install selenium]
  • Python 3+
  • Chrome Web Driver [Download]

Note: our Chrome Driver version and Chrome Browser version should be the same

Code to click a Button N Times

Here’s the python code to click a button

#click a button
from selenium import webdriver
import time
driver = webdriver.Chrome()
# open the website
driver.get('https://orteil.dashnet.org/cookieclicker/')
#wait to load
time.sleep(5)
#get the button
button = driver.find_elements_by_id(bigCookie)[0]
#200 is no of times to click 
for i in range(200):
    button.click()
    print(i, click)
#prints no.of times clicked

Button ain’t clicking?

Follow these steps and it should work

  1. Open Chrome and the website you need
  2. Right-click on the button to click
  3. select Inspect element option
  4. In the HTML code look for the id of the button

id starts with a ‘ # ‘

Looks something like this #button-five

replace bigCookie with button-five

Have a look at the cool bots I made with Selenium

This post is published as a part of my project called The Selenium Project, where I automate the boring stuff using python and selenium mostly. If you find it interesting check it out and drop a star at the GitHub Repository