Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if element is on page with selenium

I thought doing this

        if driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button"):
            pass

would work, but it doesn’t and just returns

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[3]/div[1]/div[1]/button"}"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are two main ways to do so:

Using Try / catch

try:
    driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button")
except NoSuchElementException:
    print("Element does not exist")

Using find_elements()

if(len(driver.find_elements_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button")) > 0):
    print("Element exist")
else:
    print("Element does not exist")

For more informations see: https://www.tutorialspoint.com/checking-if-element-exists-with-python-selenium

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading