Alexander Schlegel Alexander Schlegel - 1 year ago 94
Python Question

How to accept alert that is triggered by 'get' in selenium (python, chromedriver)?

I am trying to use selenium to navigate from some page to another:

driver = webdriver.Chrome()
driver.get("...some page...")
... # the alert does not exist yet and thus cannot be accepted
driver.get("...some other page...") # the alert pops up here and blocks navigation to 'some other page'
# execution never reaches here

Now, navigating away from 'some page' triggers an alert, asking to confirm that one really wants to leave the page. This blocks execution forever. An implicit timeout was set, but is not triggered by this. I cannot get selenium to accept the alert, because only appears after calling 'get'.

Is there any way around this?

Thank you very much!

Answer Source

Try following code and let me know if it's not working

from import WebDriverWait
from import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
driver.get("...some page...")
    WebDriverWait(driver, 5).until(EC.alert_is_present())
except TimeoutException:
    print("Alert not found. Move on...)
driver.get("...some other page...")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download