无法使用Java中的phantomJS处理警报

时间:2016-12-30 10:28:30

标签: java selenium phantomjs ghostdriver

我有一个Java代码,如下所示,当我正在运行PhantomJs时,得到"不支持的命令异常"但如果我运行firefox和chrome,它工作正常: -

注意:使用phantomJs,我们可以在下面的代码中执行到第3步。我在许多博客中搜索,但这些答案并没有解决我的问题。

1.      cvvField.sendKeys(cvcData);
2.      proceedToPayBtn.click();
3.      Reporter.log("Card details are submitted from payment UI page");
4.      Alert a1=driver.switchTo().alert();
5.      Reporter.log("Alert with text:"+a1.getText());
6.      a1.accept();   

这里cvvField和proceedToPayBtn是WebElements,cvcData的值为" 111"。

错误日志: -

org.openqa.selenium.UnsupportedCommandException: Invalid Command Method - 

{"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:30462","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_101)"},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{"anchor":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"

/alert_text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/alert_text","queryKey":{},"chunks":["alert_text"]},"urlOriginal":"/session/9e392a50-ce79-11e6-b24a-2b12cf1ec4d6/alert_text"}

命令持续时间或超时:31 milliseconds

  

我已编辑上面的代码如下,但同样的错误即将来临。请建议

 if (driver 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();
       }                        
  

我得到"警报已被处理"在输出控制台中,但不处理警报。

1 个答案:

答案 0 :(得分:0)

由于等待时间的某些问题可能是您的问题的根源 上面的代码可以帮助等到元素可见(因为ngWebDriver或Selenium Webdriver的等待与PhantomJS不兼容)

public static String waitJSResponse(PhantomJSDriver driver, String script) {
        String ReturnedValue = null;
        int sleeper = 10;
        Boolean flag = false;
        int timeOut = 30000;
        int i = 0;
        while ((!flag) & ((i*sleeper)<timeOut)) {
            try {
                Thread.sleep(sleeper);
                ReturnedValue = (String) driver.executeScript(script);

            } catch (Exception e) {
                flag = false;
                i++;
            }
            if (ReturnedValue != null) {
                flag = true;
                System.out.println("Overall wait time is : "+(i * sleeper)+" ms \n\r");
                break;
            }
        }
        return ReturnedValue;
    }

此代码将等待10ms然后验证该元素是否可见,如果有异常,它将再次循环。 返回的值必须是文本,对象或非空的任何内容。 脚本值必须是您的JS脚本才能获得正确的元素。

希望它有效。

我通过以下方式尝试了上述代码: -

1.创建一个“测试”类并在其中编写上述方法。 2.通过将对象(TestObject)创建为

来调用方法
  

TestObject.waitJSResponse((PhantomJSDriver)驱动程序,“window.confirm = function(){return true;};”);

中的ReturnedValue

try     {
Thread.sleep(sleeper); ReturnedValue = (String) driver.executeScript(script); System.out.println(ReturnedValue);

}

返回null.So你可以帮忙吗?