简体   繁体   English

如何使用phantomjs无头浏览器处理selenium中的警报

[英]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.

相关问题 如何使用无头浏览器处理警报(HtmlUnitDriver / Phantomjs驱动程序) - How to Handle Alerts Using Headless Browsers (HtmlUnitDriver/Phantomjs Driver) 如何使用Java处理phantomjs硒中的JavaScript错误? - How to handle javascript error in phantomjs selenium with java? 如何通过 Selenium Java 初始化 PhantomJS 浏览器 - How to initialize PhantomJS browser through Selenium Java 如何处理Selenium WebDriver自动化的背靠背警报 - How to handle back to back alerts on selenium webdriver automation 硒-在无头浏览器中设置标头 - Selenium - set headers in headless browser 如何运行无头的硒脚本,独立于主机和浏览器 - How to run headless selenium scripts, Host and Browser independent 如何使用Selenium Java中的机器人类在无头浏览器中上传文件 - How to upload file in headless browser using robot class in selenium java 使用Selenium 3在无头phantomjs 2.1.1中找不到元素 - Can't find element in headless phantomjs 2.1.1 with Selenium 3 如何在Selenium2(Webdriver)中处理POPUP浏览器? - How to Handle POPUP browser in Selenium2(Webdriver)? 我如何使用 java 处理 selenium 中的摄像头、通知和位置警报(基于 Web)? - How I can handle Camera, notifications and location alerts(web based) in selenium with java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM