Unhandled Alert Exception : Modal Dialog Present (Selenium) - java

I am facing an Issue wherein I get an Exception saying UnhandledAlertException Modal Dialog Present ,below are some of the traces
org.openqa.selenium.UnhandledAlertException: Modal dialog present:
This page is asking you to confirm that you want to leave - data you
have entered may not be saved.Build info: version: '2.41.0', revision:
'3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27
11:29:39'System info: host: 'NCESEPBLRNRE16', ip: '172.16.137.213',
os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1',
java.version: '1.6.0_26'Session ID:
bda22089-a586-4cd8-b7ac-778c98581e11Driver info:
org.openqa.selenium.firefox.FirefoxDriverCapabilities [{platform=XP,
acceptSslCerts=true, javascriptEnabled=true, browserName=firefox,
rotatable=false, locationContextEnabled=true, version=23.0,
cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true,
browserConnectionEnabled=true, nativeEvents=false,
webStorageEnabled=true, applicationCacheEnabled=true,
takesScreenshot=true}] at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at
org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:174)
at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at
org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:389)
at org.openqa.selenium.By$ById.findElement(By.java:214) at
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
at
com.amadeus.selenium.runner.SEPWebDriverWrapper.findElement(SEPWebDriverWrapper.java:141)
I searched for some solutions and got the following snippet to handled the issue
//Now the alert appears. Alert alert = driver.switchTo().alert();
alert.accept();
It works fine , but the alert popup appears in different instances (or different scenarios ) for which the alert cannot be handled in all blocks of code
Please let me know if there is any common approach wherein i can disable this alert popup permanently through some firefox profile settings or anyother generic approaches
I am using Selenium Java with browser as Firefox

Write a method like this:
protected boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
Call the above method to verify whether alert is present or not as below:
if(isAlertPresent()){
System.out.println(isAlertPresent());
driver.switchTo().alert().accept();
}

This can be solved using combination of Robot class and selenium Alert switch to command.
driver.switchTo().alert();
// invoke Robot class
Robot rb = new Robot();
rb.KeyPress(KeyEvent.VK_ENTER);
rb.KeyPress(KeyRelease.VK_ENTER);
try this out and let me know if any issues. it worked for me because I have faced thi issue earlier.

Related

WebDriver Waits not working on FindElement

WebDriver is not waiting for elements to be enabled or displayed in my web application. The web application extensively uses AJAX/Javascript for loading a very large, dynamic application, specifically Bubble's web application editor (https://bubble.is). I am developing applications using bubble and want to use Selenium/WebDriver from within the Bubble editor.
When using implicit waits, explicit waits and fluent waits, attempts to click on an element always return the following error:
org.openqa.selenium.WebDriverException: unknown error: Element <div class="tab tabs-3">...</div> is not clickable at point (30, 185). Other element would receive the click: <div class="status-notification" style="display: block;">...</div>
This issue appears to be related to elements appearing in the DOM before they are fully loaded. The following Java code is what is being used here, compiled and run in Eclipse IDE on Java 9:
public static void main(String[] args) {
try {
WebDriver chrome = bubbleLogin(false); // login to application via static page. this step works
WebDriver driver = chrome;
// load app
WebDriverWait wait = new WebDriverWait(driver, DEFAULT_TIMEOUT_IN_SECONDS);
driver.get("https://bubble.is/page?type=page&name=index&id=test123456code&tab=tabs-1");
switchToWindowWithTitle("test123456", driver);
// EXPLICIT WAIT: wait for "elementToBeClickable" to be true - element must be displayed and enabled
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.tab.tabs-3")));
// FLUENT WAIT: wait for element.IsEnabled to be true - element must be displayed and enabled
// instantiate an instance of this class to run the wait function I wrote
testng test = new testng();
test.waitUntilElementExistsAndIsEnabled(driver,By.cssSelector("div.tab.tabs-3"));
// only works via sleep... not implicit, explicit or fluent waits
// Thread.sleep(10000);
driver.findElement(By.cssSelector("div.tab.tabs-3")).click();
} catch (Exception e) {
System.out.println("failed run");
e.printStackTrace();
}
}
}
And the above FluentWait method:
private void waitUntilElementExistsAndIsEnabled(WebDriver driver, final By by) {
new FluentWait<WebDriver>(driver).withTimeout(DEFAULT_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
.pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS, TimeUnit.SECONDS).ignoring(NoSuchElementException.class)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver wd) {
return wd.findElement(by).isEnabled();
}
});
}
Here is a complete dump of the error message from Eclipse:
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.12.1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
System info: host: 'Krystals-MacBook-Air.local', ip: 'fe80:0:0:0:c33:4430:fcfd:933c%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.1', java.version: '9'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=MAC, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=MAC, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061), userDataDir=/var/folders/gr/lggvp4hn09x2zqg6k561y4fr0000gn/T/.org.chromium.Chromium.haqAAG}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: 0c9110334bb1b20a306363006f4bb868
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
at testng.main(testng.java:497)
i used
thread.sleep before i click the element and then
i will find the parent element before i click the main element
or
(new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Reset")));
boolean Sort = driver.findElement(By.linkText("Reset")).isDisplayed();
if (Sort == true){
}else{
}
hope it will works

Webdriver Expected condition failed: waiting for element to no longer be visiblr

I have a method which waits for a css (Modal) locator to not be visible on screen, on some of my builds I get the following failed message
Expected condition failed: waiting for element to no longer be
visible: By.cssSelector: .modal-body (tried for 6 second(s) with 500
MILLISECONDS interval)
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DEV007', ip: '172.16.2.192', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false,
chrome={chromedriverVersion=2.29.461591
(62ebf098771772160f391d75e589dc567915b233),
userDataDir=C:\Users\GI\AppData\Local\Temp\2\scoped_dir7780_13017},
takesHeapSnapshot=true, pageLoadStrategy=normal,
databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false,
version=58.0.3029.110, platform=XP, browserConnectionEnabled=false,
nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true,
webStorageEnabled=true, browserName=chrome, takesScreenshot=true,
javascriptEnabled=true, cssSelectorsEnabled=true,
unexpectedAlertBehaviour=}]
Session ID: eb353964f7b9bd515e527a795a111bc3
My method:
public boolean waitUntilModalDisapears() {
return this.wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
}
Loading web elements in your page will be different each time you run your code so you should increase the wait time in your web driver wait and try running your code more than once to be sure your driver have waited enough time for the element to be loaded
try below :
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
There is nothing wrong in this method and you use it right.
The code of this method (on C#) is:
return (Func<IWebDriver, bool>) (driver =>
{
try
{
return !driver.FindElement(locator).Displayed;
}
catch (NoSuchElementException ex)
{
return true;
}
catch (StaleElementReferenceException ex)
{
return true;
}
});
So probably your element was visible indeed. Try to increase your timeout and maybe to make screenshots on failure to be able to view real data and to know if the element is visible or not indeed.

org.openqa.selenium.NoSuchElementException - Selenium Test

In Java, I have written a Selenium test for testing Yahoo Movies. I am testing This Movie from yahoo website. But the code that I have written (given below) is throws an Exception (given below). I am new to Selenium, so please solve the problem.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Tests {
WebDriver driver;
Wait<WebDriver> wait;
boolean result;
Tests() {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 30);
driver.get("http://www.yahoo.com/");
}
public static void main(String arg[]) {
boolean result = new Tests().movies();
System.out.println(result?"PASSED":"FAILED");
}
public boolean movies() {
try {
System.out.print("Testing Movies... ");
driver.findElement(By.linkText("Movies")).click();
driver.findElement(By.linkText("Finding Dory")).click();
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
return webDriver.findElement(By.className("yvp-flash-video")) != null;
}
});
return driver.findElement(By.id("Main")).getText().contains("'Finding Dory' Trailer");
}
catch(Exception exp) {
exp.printStackTrace();
return false;
}
}
}
Exception:
Testing Movies... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
Command duration or timeout: 5.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
*** Element info: {Using=link text, value=Finding Dory}
Session ID: 5f14f1fa-85e4-471e-982f-27317dd766b7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=46.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Tests.movies(Tests.java:189)
at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Finding Dory"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10779)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous903980554428331931webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
FAILED
I am assumming that you are trying to click a "link" named "Finding Dory" on this page:
But this is not a link - try to click it manually, it simply is not possible to click on it.
If you click on it, nothing happens, it's just not clickeable - this is nothing but a simple text "Finding Dory".
What you can do is to click a link named "Play trailer" that is located below the text "Finding Dory". The problem is that there are many links with the same name "Play trailer" on this page, and WebDriver doesn't know which link should be clicked, and returns an error if you try a simple method findElement( By.linkText('Play trailer')).click();
You need to tell WebDriver that it should click on the link that is located under the text "Finding Dory". To do it you need more advanced locating strategies than a simle method By.linkText. The two the most popular are locating using xpath or css expressions.
First you need to examine a structure of this page. Open this page in the browser, and press F12 key - this opens Developer Tools window (how to use this tool is beyond this answer). You will see something like this:
That is:
DIV tag that contains a text "Finding Dory"
followed by A tag that contains a text "Play trailer"
You can build an xpath expression for the above, that finds a link "Play trailer" located under a DIV tag that contains "Finding Dory" in this way :
//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]
and then replace this line of your code:
driver.findElement(By.linkText("Finding Dory")).click();
with this one:
driver.findElement(
By.xpath(
"//div[contains(.,'Finding Dory')]/following-sibling::a[contains(.,'Play trailer')]"
)).click();
You can try the following code
driver.findElement(By.xpath(.//a[contains(text(),'Finding Dory'))].click();
Note: If "Finding Dory" is having any link then only the above code will work.

WebElement#getScreenShotAs(OutputType.File) not working

I am trying to use WebElement#getScreenShotAs(OutputType.FILE) feature added in selenium webdriver 2.47.0 version on Firefox Browser
Code
public static void main(String[] args) throws IOException {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
WebElement element=driver.findElement(By.cssSelector("a[title='Contact Us']"));
System.out.println(element.getText());
element.getScreenshotAs(OutputType.FILE);
File destination=new File("Image.png");
FileUtils.copyFile(null, destination);
}
..But I am getting below exception:
Contact us
Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: Unrecognized command: GET /session/e796089b-1d64-4590-9157-a0716a57e399/screenshot/%7B4329461b-5e9c-4f8b-b589-ddc1af1d55a6%7D
Command duration or timeout: 16 milliseconds
Build info: version: '2.52.0', revision: '4c2593cfc3689a7fcd7be52549167e5ccc93ad28', time: '2016-02-11 11:22:43'
System info: host: 'mrunal-laptop', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=41.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: e796089b-1d64-4590-9157-a0716a57e399
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.getScreenshotAs(RemoteWebElement.java:447)
at thirdsession.GetProperties.main(GetProperties.java:20)
The real reason for the error is that many / most WebDriver implementations do not actually support element-based screenshots, despite WebElement extending TakesScreenshot since 2.47.0. Perhaps someday this will change.
If you want screenshots you need to do them at the whole-browser level, in which case - as other answers have it - you need to pass the WebDriver instance.
File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
Strictly speaking you should probably do the following, since not all Drivers are guaranteed to support screenshots, e.g. HtmlUnitDriver.
if (!(getDriver() instanceof TakesScreenshot)) {
File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
// ...
}
There are alternate solutions for single-element screenshots, but they inevitably involve cropping of the full-browser screenshot. See, for example: https://stackoverflow.com/a/13834607/954442
Update: just to clarify that this is not a bug, it's that although element screenshots are a part of the W3C WebDriver spec, different browsers have different levels of compliance/coverage, and as far as I know this feature is only supported by Microsoft Edge.
Dont Import these lib from Suggestions,
import org.eclipse.jetty.server.Response.OutputType;
import org.seleniumhq.jetty9.server.Response.OutputType;
Import these lib.
import org.openqa.selenium.OutputType;
It should be something like this.
File screenS = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenS, new File("C:\\akshay\\del\\screenshots\\1.jpg"));
replace the above code with
element.getScreenshotAs(OutputType.FILE);
File destination=new File("Image.png");
FileUtils.copyFile(null, destination);
The getScreenShotAs() method is not defined as part of the WebElement interface. Rather, it is included as part of the TakesScreenshot interface. If you want to take a screenshot and save it to a file, try the following:
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Selenium WebDriver - Unexpected modal dialog Alert

I am trying to use WebDriver to automate an website.
I am using Firefox Driver, but the homepage has a Pop-up modal alert window: saying:
You need to use IE 6.0 for viewing this application. Else some
features may not work
I checked the Source of the page, it has a function.
The Modal Alert is not an HTML element, I tried finding any element with FireBug, but to no avail.
if ( strBrowName == "Microsoft Internet Explorer" )
{
if ( (( strBrowVersion.indexOf( 'MSIE 6' ) ) > 0 ) )
{
}
else
{
alert( "You need to use IE 6.0 for viewing this application. Else some features may not work" );
}
In my WebDriver code I am using the following capability in the Driver (as suggested by some other post here)
DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver =new FirefoxDriver(dc);
Then I am making a simple get call, enclosed in a try-catch:
try {
driver.get(B);
}
catch (UnhandledAlertException e) {
System.err.println("Caught UnhandledAlertException: ");
}
System.out.println("URL Opened");
If I do not write any method on the driver object and close the driver instead.
The program terminates in Eclipse normally, but the Modal Alert stays open, inspite of the:
UnexpectedAlertBehaviour.ACCEPT
But, if I use ANY driver related method or operation, like, as simple as getTitle:
String title = driver.getTitle();
The Java code fails with Exception, BUT the modal Alert pop-up closes!
And the last line number of the error is given as the line where I used
the first driver related operation.
Please share your thoughts...
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog (text: You need to use IE 6.0 for viewing this application. Else some features may not work): You need to use IE 6.0 for viewing this application. Else some features may not work
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'LFY2DSY1', ip: '30.142.106.199', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.0.5, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a97ab146-4929-4502-98f2-810169cc5532
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:185)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:152)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:628)
at org.openqa.selenium.remote.RemoteWebDriver.getTitle(RemoteWebDriver.java:319)
at SelPkg.CIRS.main(CIRS.java:76)
The behaviour is intended. Here is how it works -
You issue driver.get(B). It triggers the browser to open the webpage and then it doesnt have anything to do with the browser, so it doesnt care whether an alert is open or not.
When the page loads, pop-up dialog appears, but nothing happens on your code side or Eclipse.
When you try to perform another operation, it interacts with browser and sees an unexpected popup dialog.
Now, the problem occurs that modal dialog closes and still exception occurs, so try the following.
Enclose your second operation in a try/catch and handle UnhandledAlertException
Inside that catch block, perform, driver -> switch to -> alert -> accept
After the catch block, perform the second operation again.

Categories

Resources