How to check if specific div element exists in Selenium Python?

Selenium Python – Check if specific div element exists

In this tutorial, you will learn how to check if a div element with a specific locator (id, classname, etc.) exists in the webpage using Selenium in Python.

To check if a div element exists in Selenium Python, find the div element using a locator like id, class, XPath expression, etc., with find_element() method. If the method returns an element, then the div element we are looking for exists, but if the find_element() method throws NoSuchElementException, then we can say that the div element does not exist.

In the following code snippet, we use try-except to check if the div element exists with the id='mydiv1'.

try:
    element = driver.find_element(By.XPATH, "//div[@id='mydiv1']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

Make sure that in the program you import NoSuchElementException from selenium.common.exceptions.

Examples

In the following examples, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-4.html . The contents of this HTML file is given below.

<html>
  <body>
    <div id="mydiv1">Apple</div>
    <div id="mydiv2">Banana</div>
    <div id="mydiv3">Cherry</div>
  </body>
</html>
Copy

1. The div element exists

In the following program, we initialize a webdriver, navigate to a specific URL, and check if a div element with id="mydiv1" exists in the document or not.

From the HTML file, we can say that the div element exists. The program must determine the same.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-4.html')

# Check if div element exists
try:
    div_element = driver.find_element(By.XPATH, "//div[@id='mydiv1']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

# Close the driver
driver.quit()
Copy

Output

Selenium Python Example - Check if specific div element exists

2. The div element does not exist

In this example, we are going to check if there is a div with the id="mydiv87".

Since, we know that there is no such element in the document, find_element() must throw NoSuchElementException.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-4.html')

# Check if div element exists
try:
    div_element = driver.find_element(By.XPATH, "//div[@id='mydiv87']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

# Close the driver
driver.quit()
Copy

Output

Selenium Python Example - Screenshot that the div we are looking for does not exist

Summary

In this Python Selenium tutorial, we have given instructions on how to check if a specific div element exists or not, with example programs.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is a web driver in Selenium Python?

Not answered

Q3. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q4. What is the method used to interact with web elements in Selenium Python?

Not answered

Q5. What is the method used to switch to a different frame in Selenium Python?

Not answered
Code copied to clipboard successfully 👍