• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Selenium to Check if Element Exists in Python

Using Selenium to Check if Element Exists in Python

June 2, 2022 2 Comments

To check if an element exists in a web page when using the Python Selenium module, the easiest way is with the Selenium webdriver find_element() or find_elements() functions.

find_element() returns a single element if it is found, and find_elements() returns a list if the elements are found. If elements are not found, a NoSuchElementException is raised.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

try:
   content = driver.find_element(By.CSS_SELECTOR,"h1"):
   print("element found!")
except:
   print("element not found")

#Output:
element found!

The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browsers.

When working with a web page, sometimes it can be useful to check if an element exists.

The easiest way to check if an element exists in a web page is with the Selenium webdriver find_element() function. You can pass “By.CSS_SELECTOR” to find an element by a css selector.

If the element exists, then find_element() will return that element. If the element doesn’t exist, then find_element() raises a NoSuchElementException.

Therefore, you need to wrap the code in a try/except block.

Below is a simple example showing you how to check if an element exists in a web page using Selenium in Python.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

try:
   content = driver.find_element(By.CSS_SELECTOR,"h1"):
   print("element found!")
except:
   print("element not found")

#Output:
element found!

If you want to get all elements by a certain css selector, then you can use the find_elements() function. find_elements() returns a list of elements if they exist.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

try:
   content = driver.find_elements(By.CSS_Selector,"h1"):
   print("element found!")
except:
   print("element not found")

#Output:
element found!

Hopefully this article has been useful for you to learn how to check if an element exists when using Selenium in Python.

Other Articles You'll Also Like:

  • 1.  How to Get the Size of a List in Python Using len() Function
  • 2.  How to Check if Number is Negative in Python
  • 3.  Selenium minimize_window() Function to Minimize Window in Python
  • 4.  Generate Random Float Between 0 and 1 Using Python
  • 5.  Python Add Months to Datetime Variable Using relativedelta() Function
  • 6.  How to Combine Dictionaries in Python
  • 7.  Get All Substrings of a String in Python
  • 8.  Factorial Program in Python Using For Loop and While Loop
  • 9.  Python Subtract Days from Date Using datetime timedelta() Function
  • 10.  Get Substring Between Two Characters with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. Mac says

    October 5, 2022 at 12:24 pm

    This does not work for me. ‘driver.find_element_by_css_selector’ is apparently no longer valid. See https://selenium-python.readthedocs.io/locating-elements.html.
    What works for me is:
    if driver.find_element(By.CSS_SELECTOR, “.fa-bars”):
    As long as the if statement is true the code works
    However if the if statement is false it just throws an error and exits which pretty much makes the if statement pointless.
    Thanks

    Reply
    • Erik says

      October 6, 2022 at 8:22 am

      Thanks for the comment Mac, I’ve edited the post with the correct code.

      In this case, you can put it in a try/except block.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy