Python and Selenium - Disable alert when leaving page

后端 未结 2 963
后悔当初
后悔当初 2021-01-20 23:50

Using Python 3 and Chromedriver.

Suppose an automated python program is surfing the web, fetching stuff from different sources.

2条回答
  •  旧时难觅i
    2021-01-21 00:23

    Disable the alert on before unload:

    def super_get(url):
        driver.get(url)
        driver.execute_script("window.onbeforeunload = function() {};")
    

    And now use super_get(whatever_url) insetad of the standard driver.get(whatever_url)

    Disable all alerts in page:

    def super_get(url):
        driver.get(url)
        driver.execute_script("window.alert = function() {};")
    

    Hope it helps somebody. Cheers.

提交回复
热议问题