gpt4 book ai didi

java - 无法在 Java 中使用 phantomJS 处理警报

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:58 25 4
gpt4 key购买 nike

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

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

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 毫秒

I have edited above code as below but same error is coming.Please suggest

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

I am getting "Alert has been handled" in output console but alert is not handled.

最佳答案

由于等待时间而导致的某些问题可能是问题的根源上面的代码可以帮助等待元素可见(因为 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,然后验证元素是否可见,如果出现异常,则会再次循环。返回值必须是文本、对象或任何不为空的内容。script 值必须是您的 JS 脚本才能获取正确的元素。

希望它能成功。

我尝试了上面的代码:-

1.创建一个类“Test”并在其中编写上述方法。2.通过创建一个对象(TestObject)来调用上述方法

TestObject.waitJSResponse((PhantomJSDriver) driver, "window.confirm = function(){return true;};");

但返回值在

尝试 {
Thread.sleep( sleep 者);ReturnedValue = (String) driver.executeScript(script);System.out.println(ReturnedValue);

}

返回 null。您能帮忙解决一下吗?

关于java - 无法在 Java 中使用 phantomJS 处理警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41394645/

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