Effortless Button Clicks with Selenium: A Quick Guide

If you need to automate the process of clicking a button on a webpage using Selenium, this guide will walk you through it effortlessly.

Prerequisites

You will need the following items.

  • Python3 with selenium module

  • Chrome Browser and Chrome Driver [Download]

Script

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import json
import sys
import time

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")

driver = webdriver.Chrome('./chromedriver', options=chrome_options)

# load webpage
driver.get("https://example.com")

# the id of the button to be clicked
submit = driver.find_element_by_id("btn-id")
submit.click()
time.sleep(3)

# button click action
confirm = driver.find_element_by_id("confirm-button")
confirm.click()

print("Done")