Steps of Automation on “Click” element using Selenium WebDriver
While working with Selenium Web Driver one of the most common tasks we see is clicking on an element.
Step 1: Setting up the Selenium in Python.
Make sure to install Python in your system then we’ll install Selenium using the following command in the terminal.
pip install Selenium
Step 2: Installing the necessary modules.
Now that we have installed Selenium we’ll need to import Web Driver and By from Selenium.
- WebDriver: Web Driver is an important part of Selenium, it provides a way to interact with the web browser programmatically. It acts as a bridge between our automation script and the Web Browser
- By: By is a class in Selenium that provides a way to locate an element on a web page using various locating Strategies.
from selenium import webdriver
from selenium.webdriver.common.by import By
Step 3: Initializing the Web Driver and navigating to the web page.
Now that we have imported the required modules we will create an instance of the web driver and navigate to the web page.
Explanation:
webDriver.Chrome() is used to create an instance of the Selenium Web Driver and select Chrome as our automation Web Browser. driver.get(url) is a method of selenium web driver which is used to navigate to the specified url.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
Step 4: Finding an element.
In order to click on an element we first need to find an element on a webpage using Selenium Web Driver using find_element() method and BY.
- find_element()- find_element is a method in Selenium which is used to find an element using a locating Strategy. It return an instance of the element if found .
- By- By is a class in Selenium which provides a way to locate an element on web page.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "http://127.0.0.1:5500/index.html"
driver.get(url)
name_input = driver.find_element(By.NAME, "name")
Output:
Note: Here we have used name locating strategy to locate an element by its name attribute. Refer to Locating Strategies in Selenium to know more about locating Strategies.
Step 5: Click on an element.
- Now that we have locate the element we can now used .click() element to click on an element on a web page.
- .click(): It is a method in Selenium which is used to click on an element on a web page.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "http://127.0.0.1:5500/index.html"
driver.get(url)
name_input = driver.find_element(By.NAME, "name")
name_input.click()
Output:
How to Automate Click Using Selenium WebDriver?
Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions like filling out forms, clicking on a button, navigating to a web page, and many more. One of the most common tasks while working with Selenium is clicking on an element as it is widely used for interacting with a web application. So in this article, we’ll learn how to click on an element using Selenium Web Driver.
Table of Content
- How to Use the Selenium Click Command?
- Steps of Automation on “Click” element using Selenium web driver
- Advanced Click Techniques – Using send_keys() with Keys.CONTROL
- Conclusion
- FAQ’s
Contact Us