2017-07-24 84 views
1

使用Python 3ChromedriverPython and Selenium - 當離開頁面時禁用警報

假設一個自動化的python程序正在瀏覽網頁,從不同的來源獲取東西。

假設任何這些網站觸發了「你確定要離開這個頁面?」警報。

關鍵詞:任何(以隨機的方式)這些網站。

問題

請問有什麼可以設置的程序來處理這些警報,由總說:「是的,我要離開這個頁面。」?

--- UPDATE ---

可能的方法

基於以下我的評論我現在做的:

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

而現在使用super_get() insetad的標準driver.get()

C你想到更高效或更乾淨的方法嗎?

+1

https://stackoverflow.com/questions/6820377/handling-are-you-sure-you-want-to-navigate-away-from-this-page-msg-in-selenium –

+0

@cᴏʟᴅsᴘᴇᴇᴅ謝謝。我對搜索「python」的癡迷使我沒有看到java的方法。 –

回答

3

禁用警報之前卸載

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

現在在頁面使用標準driver.get(whatever_url)

禁用的super_get(whatever_url) insetad所有警報:

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

希望它幫助某人。乾杯。

+0

不適用於我 – Alex

相關問題