Checking If Element Exists With Python Selenium

Checking if element exists with Python Selenium

A) Yes. The easiest way to check if an element exists is to simply call find_element inside a try/catch.

B) Yes, I always try to identify elements without using their text for 2 reasons:

  1. the text is more likely to change and;
  2. if it is important to you, you won't be able to run your tests against localized builds.

solution either:

  1. You can use xpath to find a parent or ancestor element that has an ID or some other unique identifier and then find it's child/descendant that matches or;
  2. you could request an ID or name or some other unique identifier for the link itself.

For the follow up questions, using try/catch is how you can tell if an element exists or not and good examples of waits can be found here: http://seleniumhq.org/docs/04_webdriver_advanced.html

Selenium check if element exists without exception

Instead of driver.find_element you should use driver.find_elements method here.

Something like this:

if driver.find_elements_by_xpath("/div[@class='class_name']"):
driver.find_element_by_xpath("/div[@class='class_name']").click()

Or this:

elements = driver.find_elements_by_xpath("/div[@class='class_name']")
if elements:
elements[0].click()

driver.find_elements will return you a list of web elements matching the passed locator. In case such elements found it will return non-empty list interpreted by Python as a Boolean True while if no matches found it will give you an empty list interpreted by Python as a Boolean False

Check if element exists selenium python

Use the .find_elements (note the plural) method:

if len(driver.find_elements(By.CLASS_NAME, 'userIDSection'))>0:
# DO YOUR THING

So if there are no elements with that class name, you will get len = 0, but no error.

How to check if an element exists in the HTML using Selenium

To attempt to find an element on the pages using the class and display the text from there irrespective of the element being present or not you can wrap up the code in a try-except{} block handling the NoSuchElementException as follows:

driver.get(url=urlik)
time.sleep(2)
try:
urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text
for page_number in range(int(urla.split()[3])):
page_number = page_number + 1
driver.get(url=urlik + f"page/{page_number}")
time.sleep(2)
imgs = driver.find_elements(By.CLASS_NAME, "cGalleryPatchwork_image")
for i in imgs:
driver.execute_script("arguments[0].scrollIntoView(true);", i)
time.sleep(0.2)
print(i.get_attribute("src"))
except NoSuchElementException:
print("Element is not present")

Check if element exists python selenium

You can implement try/except block as below to check whether element present or not:

from selenium.common.exceptions import NoSuchElementException

try:
element=driver.find_element_by_partial_link_text("text")
except NoSuchElementException:
print("No element found")

or check the same with one of find_elements_...() methods. It should return you empty list or list of elements matched by passed selector, but no exception in case no elements found:

elements=driver.find_elements_by_partial_link_text("text")
if not elements:
print("No element found")
else:
element = elements[0]

Check if element exists, and do .. if not

You can do it by checking the size of the list of the WebElements, if the size is greater than 0 that means the element is present on the page else it is not present.

Your code should be like:

if len(driver.find_elements_by_xpath("//input[@name='username']"))>0:
# Element is present
print("Alert")
else:
# Element is not present
print ("Nothing")


Related Topics



Leave a reply



Submit