java phantomjs alert,无法在Java中使用phantomJS处理警报

I have a Java code as below and when I am running through PhantomJs getting "Unsupported Command Exception" but it is working fine if I run through firefox and chrome:-

Note: With phantomJs we could able to execute till 3rd step in below code.I searched in many blogs but those answers didn't solve my problem.

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

Here cvvField and proceedToPayBtn are WebElements and cvcData have value as "111".

Error log:-

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"}

Command duration or timeout: 31 milliseconds

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.

解决方案

Some issue due to wait time can be the source of your problem

The code above can help to wait until element is visible (since the wait of ngWebDriver or Selenium Webdriver are not compatible with 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)

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;

}

This code will wait 10ms then verify that the element is visble, if there is an exception, it will loop again.

The returned value must be a text, an object or anything that is not null.

the script value must be your JS script to get the correct element.

Hope it work.

I tried the above code by:-

1.Creating a class "Test" and writing above method in it.

2.Above method is called by creating an object(TestObject) as

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

But ReturnedValue in

try

{

Thread.sleep(sleeper);

ReturnedValue = (String) driver.executeScript(script);

System.out.println(ReturnedValue);

}

returns null.So Can u please help with this?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值