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:
- Find the button.
- 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.
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?