【问题标题】:Can I handle more than one modalDialog with Selenium?我可以用 Selenium 处理多个 modalDialog 吗?
【发布时间】:2017-05-20 23:37:45
【问题描述】:

我为使用 ModalDialog 窗口的旧应用程序编写 Selenium 测试。我知道如何处理一个 ModalDialog:

  1. 在打开 ModalDialog 之前,我称之为 js - 将 window pro ModalDialog 更改为经典窗口:

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

  2. 打开 ModalDialog 后,我切换到该窗口,我可以处理它:

    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;
            }
        }
    }
    

但如果我想使用第二个 ModalDialog 来执行此操作(所以我现在在第三个窗口中)我遇到了这个问题:

抛出异常: OpenQA.Selenium.NoSuchElementException:找不到元素:By.Id:btnClearSearchName

(在 Firefox 窗口中闪烁:Sending request to, Transfering something, Read something)所以很明显 Selenium 找不到元素,因为页面没有加载。

如果我之前尝试在没有 JS 执行器的情况下打开第三个窗口,则会显示此错误:

抛出异常: System.Reflection.TargetInvocationException:调用的目标已引发异常。 ---> OpenQA.Selenium.WebDriverException:对 URL http://localhost:7057/hub/session/c7e75043-9605-4f7c-80ac-233803527709/element/%7B7664f1ae-9c42-4de6-9e16-34fede6a9e26%7D/click 的远程 WebDriver 服务器的 HTTP 请求在 60 秒后超时。 ---> System.Net.WebException:操作已超时 在 System.Net.HttpWebRequest.GetResponse()

我使用 Selenium v​​3.0.1 和 FireFox 45.6.0。

感谢您的帮助!

【问题讨论】:

    标签: javascript c# selenium showmodaldialog


    【解决方案1】:

    为了能够处理下一个窗口,您应该将焦点切换回默认内容

    _driver.SwitchTo().DefaultContent();
    

    【讨论】:

    • 我应该把它放在哪里?现在我在第一个模态中看起来像这样: 1. 通过 JSExecutor 将模态更改为窗口 2. 单击以打开模态(现在是窗口,因为 1.) 3. 切换到新窗口----第二个模态:-(如果我再试一次 JSExecutor = 第二个模态将不会打开)- 如果我不尝试 JSExecutor 并直接打开第二个模态窗口,= 超时错误
    • 我想你应该尝试在break; 之前放置一个,在else 块中放置一个
    • 它会出现同样的错误......在第二个窗口之前使用 JSExecutor 它不会打开窗口和闪烁请求......没有 JSExcecutor 窗口无法处理......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多