How to check "is element present" using Python client framework

Hi guys!

I’m new to appium. I’m trying to locate whether a separate element is presented in UI hierarchy on the screen using python bindings, and if “FALSE”, try some other action.

But there are no appropriate methods for that. I tried is_displayed(), is_enabled(), is_selected(), but nothing.

is_element_present =  driver.find_element_by_id("ID").is_displayed()

is_element_present = driver.find_element_by_id("ID").is_enabled()

is_element_present = driver.find_element_by_id("ID").is_selected()

These 3 don’t do I really want. find_element_by_id method always raises NoSuchElementException. Is it the only way to work around it using next:

    return true
except NoSuchElementException:
    return false


1 Like

You made the same discovery I did.

You simply call driver.find_element_by… whatever method you’ve decided to use. It’s important to note that it will return True if the element is found anywhere in the page XML, including if it is physically out of view.

When you say this,

Do you mean it raises an exception whether or not it finds the element, or only if it doesn’t find the element. Your last code example implies the latter, and that is how it behaves.

Try using find_elements, which returns a list and there’s no exception when its empty.

You would need to search for elements, then iterate through the list and check whether an element .is_displayed (you could also check if list is >0 before iteration).

yep, exactly! I mean it raises an exception when it unable to find an element. And the question is how to get a Boolean flag, validating this kind of situation?

oh, thanks! that’s a good one

OK, this is making me feel like an idiot. Am I simply not seeing some well written documentation that details find_elements vs find_element?

And I found my own answer by referring to the Selenium documentation here:

try with given findByElements suggestion. we use same with Java like (we know that only ONE exists with such ID. if many need to update code.):

if (!driver.findElements(By.Id(“some_ID”)).isEmpty && driver.findElements(By.Id(“some_ID”)).get(0).isDispalyed() ) {
= element Exists AND is visible

if element NOT exist then next “isDisplayed()” will not check and will not cause any exception.

@Christopher_Graham, ye! you’re right. As far as i discovered, findElements explicitly returns a list type in python or NoSuchElementException

@Aleksei, that’s a quite tricky one))
unfortunately there is no isEmpty method in python client framework and as far as i understood, you need to implement implicitly the custom one