如何在 selenium webdriver 中处理意外的 'alert'

how to handle unexpected 'alert' in selenium webdriver

PS: driver.switchTo().alert().accept();不工作

嗯,我想自动测试显示 随机警报*

的网页
*random alerts :JavaScript Alert which are not controlled and can appear any time 

所以我不知道放在哪里driver.switchTo().alert().accept()因为警报的发生是事先不知道的。

请帮助我..这是简单的 mocha selenium 测试...我已经注释掉了 alert.accept() 部分..

我想知道怎么处理

UnexpectedAlertOpenError: unexpected alert open: {Alert text :}

一劳永逸....

我阅读了 selenium 文档,但找不到任何...

这是我的代码

describe( 'handling alerts' , function(done){      
        after(function(done){
               return driver.quit();
            done(); 
        });

        it( 'should check title ', function(done){
                driver.get("https://www.google.in");
                driver.findElement(By.id("q")); 

                driver.get("https://jsfiddle.net/cwthh0y7/");
               // driver.switchTo().alert().accept();   

                driver.getTitle().then(function(title){  

                    assert.equal(title, "Edit fiddle - JSFiddle");
                    done();   
                });



        });

    });

您可以在定义驱动程序对象时设置功能。

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html#UNEXPECTED_ALERT_BEHAVIOR

请参阅此 link 以了解如何设置功能。这将帮助您定义间歇性警报时的通用行为。