Can I handle more than one modalDialog with Selenium?

290 views Asked by At

I write Selenium test for old application which use ModalDialog windows. I know how to handle with one ModalDialog:

  1. Before opening ModalDialog I call this js - to change window pro ModalDialog to classic window:

    ((IJavaScriptExecutor) _driver).ExecuteScript("window.showModalDialog = window.open;");

  2. After opening ModalDialog I switch to that window and I can handle with it:

    public static void SwitchToWindow(IWebDriver _driver, string url)
    {
        String parentWindowHandle = _driver.CurrentWindowHandle;
    
        IWebDriver popup = null;
        var windowIterator = _driver.WindowHandles;
    
        foreach (var windowHandle in windowIterator)
        {
            popup = _driver.SwitchTo().Window(windowHandle);
    
            if (popup.Url.Contains(url))
            {
                break;
            }
        }
    }
    

But if I want to do that with second ModalDialog (so I am in third window now) I got this issue:

threw exception: OpenQA.Selenium.NoSuchElementException: Could not find element by: By.Id: btnClearSearchName

(in Firefox window is flashing: Sending request to, Transfering something, Read something) so it's clear that Selenium cant find element, because the page isn't loaded.

And If I tried to open third window without JS executor before, it shows this error:

threw exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:7057/hub/session/c7e75043-9605-4f7c-80ac-233803527709/element/%7B7664f1ae-9c42-4de6-9e16-34fede6a9e26%7D/click timed out after 60 seconds. ---> System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()

I use Selenium v3.0.1 with FireFox 45.6.0.

Thanks for your help!

1

There are 1 answers

3
Andersson On

To be able to handle next window you should switch focus back to default content with

_driver.SwitchTo().DefaultContent();