How to click on a button using Selenium

Selenium can automatically click on buttons that appear on a webpage. To do this there are two major steps we have to take:

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

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

Syntax:  

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

# clicking on the button
button.click()

To click a button using Selenium, first locate the element that you want to click and perform some action, then use the click() method to act.

Here’s an example in Selenium Java.

Python
import time
# importing webdriver from selenium
from selenium import webdriver

# Here Chrome  will be used
driver = webdriver.Chrome()

# URL of website
url = "https://www.w3wiki.org/"

# Opening the website
driver.get(url)

# getting the button by class name
button = driver.find_element_by_class_name("slide-out-btn")

# clicking on the button
button.click()


This will click on the button and a popup will be shown. 

How to click a button on webpage using Selenium?

This article is all about how to click any button using Selenium on a webpage and many more concepts related to the same which are discussed below.

Table of Content

  • What is Selenium?
  • How to click on a button using Selenium
  • Conclusion
  • Frequently Asked Questions on How to click a button on webpage using selenium?

Similar Reads

What is Selenium?

Selenium is a widely used tool for testing web-based applications that checks if they are doing as expected. It is a prominent preference amongst testers for cross-browser testing and is viewed as one of the most reliable systems for web application automation evaluation. Selenium is also platform-independent, so it can provide distributed testing using the Selenium Network....

How to click on a button using Selenium

Selenium can automatically click on buttons that appear on a webpage. To do this there are two major steps we have to take:...

Conclusion

In conclusion, Selenium is a powerful tool for automating web application testing with using the different languages. with locating elements and using the click() method, testers will efficiently automate button clicks and interactions....

Frequently Asked Questions on How to click a button on webpage using selenium?

How to click specific button in Selenium?...