P
P
Python Newbie2021-11-09 11:18:09
Python
Python Newbie, 2021-11-09 11:18:09

How to check if an element is on a page in selenium python using if?

How to do it with no try and except

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2021-11-09
@Levman5

Now it's fashionable something like this:

driver.implicitly_wait(15)
if bool(driver.find_elements(by="class name", value='blabla')):
    print('Yesss')
else:
    print('Nooo')

and without any try

L
LordOftheCode, 2021-11-09
@LordOftheCode

def check_exists_by_xpath(xpath):
    try:
        webdriver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False
    return True

* Saved
to the function you pass xpath and it returns True or False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question