S
S
Slytherin2020-01-03 12:05:41
Python
Slytherin, 2020-01-03 12:05:41

How to check if an element is present on a website page using selenium?

I have this code for checking an element, this element is not a button, but I still click on it.

def saf():
                try:
                    driver.find_element_by_xpath("//span[@id='progressMessage']").click()
                    time.sleep(1)
                    saf()
                except:
                    pass         
saf()

The essence of this code is to wait until the element disappears from the page, an error will appear accordingly and the code will continue to be executed. Just searching through find does not work, because it shows that the element is always there, even if it is not on the page, it simply finds it in the html code of the page. What can I replace the click with to make the code work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1001001, 2020-01-03
@Slytherin

Use Explicit Expectations

from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.XPATH, "//span[@id='progressMessage']"))

or other suitable test

A
Andrey, 2020-01-03
@anerev

from selenium.common.exceptions import NoSuchElementException
while True:
    try:
        webdriver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return True
        break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question