How to deal with ModalDialog using selenium webdriver? How to deal with ModalDialog using selenium webdriver? selenium selenium

How to deal with ModalDialog using selenium webdriver?


Use

following methods to switch to modelframe

driver.switchTo().frame("ModelFrameTitle");

or

driver.switchTo().activeElement()

Hope this will work


What you are using is not a model dialog, it is a separate window.

Use this code:

private static Object firstHandle;private static Object lastHandle;public static void switchToWindowsPopup() {    Set<String> handles = DriverManager.getCurrent().getWindowHandles();    Iterator<String> itr = handles.iterator();    firstHandle = itr.next();    lastHandle = firstHandle;    while (itr.hasNext()) {        lastHandle = itr.next();    }    DriverManager.getCurrent().switchTo().window(lastHandle.toString());}public static void switchToMainWindow() {    DriverManager.getCurrent().switchTo().window(firstHandle.toString());


Try the below code. It is working in IE but not in FF22. If Modal dialog found is printed in Console, then Modal dialog is identified and switched.

 public class ModalDialog {        public static void main(String[] args) throws InterruptedException {            // TODO Auto-generated method stub            WebDriver driver = new InternetExplorerDriver();            //WebDriver driver = new FirefoxDriver();            driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");            String parent = driver.getWindowHandle();            WebDriverWait wait = new WebDriverWait(driver, 10);            WebElement push_to_create = wait.until(ExpectedConditions                    .elementToBeClickable(By                            .cssSelector("input[value='Push To Create']")));            push_to_create.click();            waitForWindow(driver);            switchToModalDialog(driver, parent);        }        public static void waitForWindow(WebDriver driver)                throws InterruptedException {            //wait until number of window handles become 2 or until 6 seconds are completed.            int timecount = 1;            do {                driver.getWindowHandles();                Thread.sleep(200);                timecount++;                if (timecount > 30) {                    break;                }            } while (driver.getWindowHandles().size() != 2);        }        public static void switchToModalDialog(WebDriver driver, String parent) {                 //Switch to Modal dialog            if (driver.getWindowHandles().size() == 2) {                for (String window : driver.getWindowHandles()) {                    if (!window.equals(parent)) {                        driver.switchTo().window(window);                        System.out.println("Modal dialog found");                        break;                    }                }            }        }    }