How to deal with ModalDialog using selenium webdriver?

后端 未结 8 1743
天命终不由人
天命终不由人 2020-12-01 14:33

I am unable to switch to Modal Dialog of given example

http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm

I don\'t kno

相关标签:
8条回答
  • 2020-12-01 15:34

    Use

    following methods to switch to modelframe

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

    or

    driver.switchTo().activeElement()
    

    Hope this will work

    0 讨论(0)
  • 2020-12-01 15:35

    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;
                        }
                    }
                }
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题