K
K
ksvdon2015-12-30 18:24:38
linux
ksvdon, 2015-12-30 18:24:38

How to check if an element exists on a page using python+selenium?

I go to a page where there can be either a sign with different options or an input field and a confirm button. If there is no input field - I want to be able not to run into a traceback that they say

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:
.
for this I thought to use try, except. They say
try:
browser.find_element_by_name('') ​​#they found the input field, entered what they wanted, confirmed with the button and that's all and got their plate with options; or if there is no input field, but there is a plate with options right away - look for what I need.
except selenium.common.exceptions.NoSuchElementException: #on except I tried to catch this piece, but apparently it's not possible. It is not described in the exceptions ... How to be?
Who has already run into similar moments when I assume that there is no element and just want to perform ANOTHER action if there is no element, and not fall into traceback.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nerevar_soul, 2015-12-30
@ksvdon

from selenium.common.exceptions import NoSuchElementException
try:
do something with the element
except NoSuchElementException:
do something else
This scheme works for me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question