2017-07-26 33 views
1

使用的Python 3>ChromedriverPython和硒 - 禁用所有警報

我正在運行一個python腳本來從不同的網站刮東西。

我需要打開和關閉很多不同的網站。

問題

我的程序崩潰時,警報被觸發。

selenium.common.exceptions.UnexpectedAlertPresentException:警報文本:無 消息:意外警告開放

我是什麼試圖解決的情況呢?

當我做driver.get(),我立即運行:

driver.execute_script("window.alert = null;") 
driver.execute_script("Window.prototype.alert = null;") 

權做driver.close()之前,我跑:

driver.execute_script("window.onbeforeunload = null;") 

右後做事driver.close(),我這樣做:

from selenium.common.exceptions import NoAlertPresentException 

... 

driver.close() 
# any alert asking me if I really want to close? 
try: 
    driver.switch_to.alert.accept() 
except NoAlertPresentException: 
    pass 

我在做什麼?

沒什麼,程序總是崩潰:

enter image description here

問題

請問有什麼可以設置整個事情,要禁用每個警報。

如果不可能,我該如何接受他們,或者至少這樣做才能使程序保持流暢?

+0

[關閉彈出窗口並警告窗口硒網絡驅動器Firefox的配置文件]的可能重複提到的網站被採取stackoverflow.com/questions/20974729/selenium-web-driver-firefox-profile-disable-popup-and-alert-windows) –

+0

當我訪問該網站時,我沒有看到彈出窗口。你能翻譯什麼彈出所有關於?謝謝 – DebanjanB

+0

@DebanjanB這是典型的「你確定要離開這個頁面嗎?」彈出。它恰好有時只出現。 –

回答

-5

這已問了很多次,你問過這個問題之前你有做過任何研究嗎? //:(HTTPS -

這會幫助你一起,它從下面

public static void main(String[] args) { 
    WebDriver driver = new FirefoxDriver(); 
    driver.get("http://toolsqa.wpengine.com/handling-alerts-using-selenium-webdriver/"); 
    driver.manage().window().maximize(); 
    // This step will result in an alert on screen 
    driver.findElement(By.xpath("//*[@id='content']/p[4]/button")).click(); 

    Alert simpleAlert = driver.switchTo().alert(); 
    String alertText = simpleAlert.getText(); 
    System.out.println("Alert text is " + alertText); 
    simpleAlert.accept(); 
} 

Source

+2

你願意'翻譯'它爲Python嗎? –

+0

是的,我做了我的研究。你沒有提供任何我沒有試過的東西,你可以在我的問題中看到。 –

+0

@ÁlvaroN.Franz我不同意,你沒有嘗試設置任何ChromeOptions。保持downvoting。 –