Reputation: 11
I am trying to handle popup using phantomjs as a driver and I want to copy the text of alert/popup in variable.
I write the code:
But I am getting exception:Exception in thread "main" java.lang.NullPointerException
Anyone knows how to handle popup/alert using phantomjs with webdriver.
I write the code:
js.executeScript("window.alert = function(msg){JavascriptExecutor js=(JavascriptExecutor) driver;
document.lastAlert=msg;};");
Object text = js.executeScript("return document.lastAlert");
System.out.println(text.toString());
Upvotes: 1
Views: 765
Reputation: 2148
Selenium has methods to interact with javascript alerts. You can interact with a javascript alert as follows:
Alert alert = driver.switchTo().alert();
From here on you can get the alert text with:
String alertText = alert.getText();
You do not need any javascript to do this. Just plain Java code does all that for you.
Upvotes: 1