How to click a button on webpage using selenium ?

Selenium can automatically click on buttons that appear on a webpage. This article revolves around how to click any button using Selenium in a webpage. In order to do this there is two major steps we have to take :

  1. Find the button.
  2. Click on the button.

We can find the button on web page by using methods like find_element_by_class_name(), find_element_by_name(), find_element_by_id() etc, then after find the button/element we can click on it using click() method.

Syntax :

# finding the button using ID
button = driver.find_element_by_id(ID)

# clicking on the button
button.click()

Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

import time
# importing webdriver from selenium
from selenium import webdriver
  
# Here Chrome  will be used
driver = webdriver.Chrome()
  
# URL of website
  
# Opening the website
driver.get(url)
  
# geeting the button by class name
button = driver.find_element_by_class_name("slide-out-btn")
  
# clicking on the button
button.click()

chevron_right


This will click on the button and popup will be shown.
Output –

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.