Home > Net >  How to make Python wait for popup in website?
How to make Python wait for popup in website?

Time:10-22

Sorry if this is a dumb question.

I'm trying to use selenium webdriver with Python to log in to a website. But on this website, once I press the login button a popup in the website shows up and I have to click a button on the popup. The website was not made very well, and the time it takes for the popup to show up in the website varies each time, sometimes it takes a few seconds and sometimes it could take 15-20. I was wondering if there was a way I could make Python wait until it detects the popup or the button I need to press on screen, then continue with the function and click the button.

CodePudding user response:

You can and should use explicit waits on this case.
For example if the button closing the pop up can be located with button.close-pop-up selector you can use this command:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.close-pop-up"))).click()

To use it you will need to import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

And initialize the wait object by

wait = WebDriverWait(driver, 20)

For an example code see this discussion or any other selenium code utilizing "expected conditions"

  • Related