How to check if an element exists with Python Selenium?
Selenium is one of the most powerful and widely used tools for web automating web applications. Whether you're a software developer or a QA tester, Selenium is an important tool to have in your toolkit. Selenium is widely used for automating user interactions like clicking buttons, filling out the forms and many more but in order to automate user interactions we first need to check if an element exists on a web page.
How to Check if an Element Exists with Python Selenium
Selenium is widely used for automating user interactions on a web page for testing or getting information from a web page. But in order to automate user interactions on a web page we first need to check if a particular element exists on a web page or not as we cannot click a button until it exists on a web page. So, in this article, we'll show you a step-by-step tutorial on how to check if an element exists on a web page with Python and Selenium.
Prerequisite
STEP-1. Setting up the Development Environment
1. Make sure that you have Python installed in your System
2. Install the Python Selenium Package- Go to your terminal and write 'pip install Selenium'
3. In the latest versions of Selenium we don't need to download Web Drivers .
You can refer to a detailed guide on How to Install Selenium in Python
STEP-2 . Installing the Libraries.
We need to import Web Driver, By and NoSuchElementException from Selenium .
- Web Driver- A web driver acts as a bridge between your testing code ( Python , Java or any other language) and your web browser you want to automate. It provides a way to control the web browser programmatically.
- NoSuchElementException - No such element exception is a common exception that occurs when the Selenium web Driver is unable to locate a element on a web page using the specified locator.
- By- By is a class that provides a way to locate a web element on a web page using various locators.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
Approach
"In step 1, we'll initialize the Selenium Web Driver and navigate to the web page using the driver.get("www.google.com") method. Here, 'driver' refers to the initialized Web driver.
In the next step, we'll try to locate the element whose presence we want to check using various Locating Strategies. Now that we have located the element, we'll check its presence using driver.find_element() with a suitable locating strategy. Here, 'driver' still refers to the initialized web driver. find_element() returns the element of class Selenium Web driver; otherwise, it throws a NoSuchElementFound exception, so we'll use Exception Handling to tackle that."
Implementation
STEP-1. Initialize the Web Web Driver And Navigate to the Web page.
Now we have to initialize the web driver and navigate to the web page, in this article we'll use Chrome Web browser . Navigate to the website that you want to check the presence of an element
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com" #Replace it with the url of your website
driver.get(url)