I have searched many, many places for a solution to my problem, but haven't found it. I figured that by now, Selenium would have provided a straight forward and simple solution to handling modal windows/dialogs from Internet Explorer using Java.
The web application that I am testing has the following characteristics:
This is what I've tried:
getWindowHandles()
always returns 1 for the parent window, so this makes driver.switchTo(handle)
not-applicabledriver.switchTo().alert()
or accept()
do not workfindElement(By whatever)
will NOT find any elements in the pop up window (like the "OK" button or the checkbox, etc.)Robot
class is the only thing that I have seen work, where I can send keypresses to navigate to the "OK" button and click it to dismiss the window...Here is my issue:
isEnabled()
, but even though items are not manually "clickable" while the modal pop up window is displayed, isEnabled()
always returns TRUE--so this does not work--is there a better way to check for this for the "blocked" elements in the background?My questions:
Thank you.
You should use WebDriverWait
with some expected condition. For example,
WebDriverWait wait = new WebDriverWait(driver, 5); // sets timeout to 5 seconds
wait.until(...); // Use ExpectedCondition to set the condition you need to check for (i.e. element to be clickable, frame to be visible, etc.)
// Do your thing.
The until
method will return an object type relative to the function passed. For example, until(ExpectedConditions.elementToBeClickable(...));
will return a WebElement
object you can use to exert an action on (like clicking on it).
Lastly, you should wrap those lines in a try/catch and handle the TimeoutException
the wait method will throw if the condition never arises.
To summarize, structurally, your code should look something like this:
// instantiate the WebDriver
...
int timeoutMax = 2; // The maximum number of seconds you wish to wait before timing out (let's assume 2 seconds is reasonable for your case)
try {
By checkboxLocator = By.id("checkboxID"); // Locate element by some criteria (id, css, xpath). Using by ID for illustration purposes only
By buttonLocator = By.id("buttonID"); // same as above
By popupLocator = By.id("frameid"); // same as above
WebDriverWait wait = new WebDriverWait(driver, timeoutMax);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(popupLocator)); // assuming it is an iframe
// The next lines will not be executed if the "Don't display this again" and clicking "OK" were clicked before (locating the frame will timeout because it not be visible)
WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
WebElement okBtn = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
checkbox.click();
okBtn.click();
driver.switchTo().defaultContent(); // Switch back to default window
} catch (TimeoutException exc) {
// Handle exception (i.e. log a warning) - This should be thrown as long as the modal dialog doesn't become visible
// If modal dialog ever becomes visible again, clicking on the checkbox and OK button will be executed again.
}
Something like this should work. Of course, this make some assumptions that might not be true for your case. However, if you use the right locating technique for your modal dialog, you should be able to:
WebDriverWait
to setup your timeout conditionsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With