• 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
  • VBA
  • 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 Leave a Comment

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_elements_by_css_selector() function.

from selenium import webdriver

driver = webdriver.Chrome()

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

if driver.find_elements_by_css_selector("h1"):
   print("element 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_elements_by_css_selector() function.

If the element exists, then find_elements_by_css_selector() will return a list with those elements. If the element doesn’t exist, then find_elements_by_css_selector() function will return an empty list.

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/")

if driver.find_elements_by_css_selector("h1"):
   print("element found!")

#Output:
element found!

One thing to note here is that you need to use the find_elements_by_css_selector() function and not find_element_by_selector().

With the latter, if the element is not found, then an exception will be thrown and your code will have an error.

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.  Using if in Python Lambda Expression
  • 2.  How to Sort Numbers in Python Without Sort Function
  • 3.  Get First Digit in Number Using Python
  • 4.  math gcd Python – Find Greatest Common Divisor with math.gcd() Function
  • 5.  Scroll Up Using Selenium in Python
  • 6.  Remove Empty Strings from List in Python
  • 7.  Find Quotient and Remainder After Division in Python
  • 8.  Write Float to File Using Python
  • 9.  Python Round to Nearest 10 With round() Function
  • 10.  Length of Tuple Python – Find Tuple Length with Python len() Function

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

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy