[英]how to handle alerts in selenium with phantomjs headless browser
Selenium version : 3.4.0 phantomjs version : 2.1.1 java : 1.8.0_151 Selenium版本:3.4.0 phantomjs版本:2.1.1 java:1.8.0_151
I have tried below things but it didnt work for me. 我尝试了下面的东西,但它对我没有用。
enter code here 在这里输入代码
1: 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: 2:
/*((PhantomJSDriver)driver).executeScript("window.alert = function(){}");
((PhantomJSDriver)driver).executeScript("window.confirm = function(){return true;}");*/
3: 3:
PhantomJSDriver phantom = (PhantomJSDriver) driver;
phantom.executeScript("window.confirm = function(){return true;};");
4: 4:
((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");
((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");
5 : 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;" +"};");
Can you please suggest apart from above. 除了上面,请你建议吗?
To handle alert in PhantomJS you have to wait for the alert to be present and then cast the WebDriver to JavascriptExecutor . 要在PhantomJS中处理警报,您必须等待警报出现 ,然后将WebDriver强制转换为JavascriptExecutor 。 You can use can use the following code block : 您可以使用可以使用以下代码块:
((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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.