How to determine the existence of a selenium element in Python

If you use an element that does not exist in Selenium, you will get a Message: Unable to locate element: xxx error. I want to check the existence of the element in advance.

Occasionally, I see a method of handling exceptions with the following try, but in fact it can also be judged with len.

try:
    driver.find_element_by_id('test').click()
except:
  #not exist

The point is to use driver.find_elements_by_id instead of driver.find_element_by_id. At first glance, it looks the same, but in the former case, an abnormality occurs when the element does not exist. In the latter case, the anomaly does not occur even if the element does not exist. Specifically, it is used as follows.

if len(driver.find_elements_by_id('test') > 0) :
    ele = driver.find_element_by_id('test')
 #Processing when it exists
else:
  #Processing when it does not exist

Recommended Posts

How to determine the existence of a selenium element in Python
How to identify the element with the smallest number of characters in a Python list?
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to get the number of digits in Python
How to pass the execution result of a shell command in a list in Python
[python] How to sort by the Nth Mth element of a multidimensional array
How to get a list of files in the same directory with python
How to use the __call__ method in a Python class
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
How to find the first element that matches your criteria in a Python list
How to get a stacktrace in python
How to know the internal structure of an object in Python
[Introduction to Python] How to use the in operator in a for statement?
Implement a deterministic finite automaton in Python to determine multiples of 3
How to get the vertex coordinates of a feature in ArcPy
How to quickly count the frequency of appearance of characters from a character string in Python?
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to calculate the volatility of a brand
How to use the C library in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
Summary of how to import files in Python 3
Get the caller of a function in Python
How to create a JSON file in Python
Make a copy of the list in Python
Summary of how to use MNIST in Python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
Output in the form of a python array
[Python] How to draw a histogram in Matplotlib
Get a capture of the entire web page in Selenium Python VBA
How to send a visualization image of data created in Python to Typetalk
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Python code to determine the monthly signal of a relative strength investment
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
How to sort by specifying a column in the Python Numpy array.
Various ways to read the last line of a csv file in Python
How to retrieve the nth largest value in Python
How to convert / restore a string with [] in python
How to mention a user group in slack notification, how to check the id of the user group
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to know the current directory in Python in Blender
[Python] How to expand variables in a character string
How to count the number of elements in Django and output to a template
How to write a list / dictionary type of Python3
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to download files from Selenium in Python in Chrome
How to find the coefficient of the trendline that passes through the vertices in Python
Python + selenium to GW a lot of e-mail addresses
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
How to develop in Python
How to execute a command using subprocess in Python
A reminder about the implementation of recommendations in Python