I'm using selenium in Python 2.7 and I have this code, but I'm looking for a more efficient way to do this:
while True:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'button'))
)
except:
break
To check if an element no longer exists on the page, we can take the help of the expected condition invisibilityOfElementLocated. To implement explicit wait conditions, we have to take help of the WebDriverWait and ExpectedCondition class.
Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.
implicitlyWait. Deprecated. Use implicitlyWait(Duration) Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
You can use waits. Check for more information in Selenium waits. In the example below we are waiting 10 seconds for the element to be visible, using the function visibility_of_element_located. Save this answer.
element = WebDriverWait(driver, 10).until(
EC.invisibility_of_element_located((By.ID, 'button'))
you don't need to use while. it already waits for time that you present in WebDriverWait() function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With