gpt4 book ai didi

java - 如何使用 phantomjs headless 浏览器处理 selenium 中的警报

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:28 24 4
gpt4 key购买 nike

Selenium 版本:3.4.0phantomjs 版本:2.1.1java: 1.8.0_151

我尝试了以下方法,但对我不起作用。

在此输入验证码

1:

webDriver instanceof PhantomJSDriver){
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("window.alert = function(){};");
je.executeScript("window.confirm = function(){return true;};");
System.out.println("Alert has been handled"); }
else {
Alert a1 = driver.switchTo().alert();
a1.accept();
}

2:

      /*((PhantomJSDriver)driver).executeScript("window.alert = function(){}");
((PhantomJSDriver)driver).executeScript("window.confirm = function(){return true;}");*/

3:

      PhantomJSDriver phantom = (PhantomJSDriver) driver;
phantom.executeScript("window.confirm = function(){return true;};");

4:

    ((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");
((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");

5:

PhantomJSDriver phantom = (PhantomJSDriver)driver;
phantom.executePhantomJS("var page = this;" +
"page.onAlert = function(msg) {" +
"console.log('ALERT: ' + msg);" +
"};");

phantom.executePhantomJS("var page = this;" +
"page.onConfirm = function(msg) {" +
"console.log('CONFIRM: ' + msg);"+ "return true;" +"};");

除上述之外,您能否提出建议。

最佳答案

要在 PhantomJS 中处理警报,您必须等待 警报出现,然后将 WebDriver 转换为 JavascriptExecutor。您可以使用以下代码块:

((JavascriptExecutor) driver).executeScript("window.alert = function(msg){};");
//or
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");
driver.findElement(By.id("element_which_trigers_the_alert")).click();

关于java - 如何使用 phantomjs headless 浏览器处理 selenium 中的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com