PhantomJSDriver Accept Alert

How can I accept an alert using PhantomJSDriver in Java? I am trying to do this from YouTube. I can not make it work.

I tried using this code for adoption on any driver, but it does not work with PhantomJS.

static void confirmDialog(WebDriver driver) { if (driver instanceof PhantomJSDriver) { PhantomJSDriver phantom = (PhantomJSDriver) driver; phantom.executeScript("window.confirm = function(){return true;}"); phantom.executeScript("return window.confirm"); } else driver.switchTo().alert().accept(); } 
+6
source share
2 answers

You must run JS to set the window.alert call to do nothing. You can use this method.

 static void confirmDialog(WebDriver driver) { if (driver instanceof PhantomJSDriver) { PhantomJSDriver phantom = (PhantomJSDriver) driver; phantom.executeScript("window.alert = function(){}"); phantom.executeScript("window.confirm = function(){return true;}"); } else driver.switchTo().alert().accept(); } 
+7
source

JavascriptExecutor worked for me. Just make sure you execute it before clicking the event that triggers the warning.

 ((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }"); 

Note: - do not use it after clicking on an event that brings up a warning confirmation window. Above the default code, set the confirmation field as true means that you accept / click ok in any confirmation window on this page, if called

Hope this helps you :)

+2
source

Source: https://habr.com/ru/post/981136/


All Articles