SwitchTo for ui-dialog with webdriver c#

时间:2014-03-17 16:02:10

标签: c# selenium dialog selenium-webdriver

我测试网站的登录部分出现在一个对话框中,该对话框似乎不是一个新窗口弹出窗口或警报(我试过SwitchTo()Alert()但它没有将其检测为警报;我不知道iframe或窗口的名称要切换到它,因为它似乎不是那种对话框)。

有没有办法可以切换到这样定义的东西,以便找到用户名和密码文本字段,以便我可以登录?

   <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" style="display: block; z-index: 1002; outline: 0px none; height: auto; width: auto; top: 181px; left: 304.5px;" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-login-box"></div> 

我是否应该尝试使用FindElement(By.xpath())直接查找元素(用户名和密码)?

我的代码是:

        IWebElement login = driver.FindElement(By.Id("login-button"));
        login.Click();

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
        wait.Until<bool>((d) =>
        {
            return driver.FindElement(By.Id("username")).Displayed;
        });
        IWebElement username = driver.FindElement(By.Id("username"));

        nickname.SendKeys("some_user");
        IWebElement password = driver.FindElement(By.Id("pass"));
        password.SendKeys("some_password");

如果我不等待显示用户名,它会告诉我:

        NoSuchElementException was unhandled: Unable to locate element: {"method":"id", "selector":"username"}. 

否则等待超时。

0 个答案:

没有答案