How to use 'expected conditions' to check for an element in python-selenium?

后端 未结 5 1773
不思量自难忘°
不思量自难忘° 2021-01-12 16:59

I have trouble understanding how to use an \'expected conditions\' to check for the presence of an element. Given this documentation it is not clear at all how to use this.

5条回答
  •  一个人的身影
    2021-01-12 17:15

    The way expected conditions work is by defining a WebDriverWait. You create this with an instance of your WebDriver and a timeout. The WebDriverWait has an until() method which will take an expected condition and will wait until it has been met or until the timeout has passed. So instead of just EC.visibility_of_element_located((By.XPATH, '//*[@id="kernel_indicator_icon" and @title="Kernel Idle"]')) you should use:

    WebDriverWait(yourdriver, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="kernel_indicator_icon" and @title="Kernel Idle"]')))
    

    Edit I should note that this doesn't return True or False. This will return the WebElement if it has been found and is visible. Otherwise it will raise a TimeOutException. Source

提交回复
热议问题