【问题标题】:WebDriverWait - Timeout isn't respected with UWPWebDriverWait - UWP 不遵守超时
【发布时间】:2018-06-21 20:45:21
【问题描述】:

我正在为 UWP(通用 Windows 平台)编写一些用于自动化测试的代码,有时我需要等待某个元素。但是我遇到了以下问题:超时时间总是少于 60 秒(40~60 秒)。

函数调用:

waitForElementName(TimeSpan.FromSeconds(120).Seconds, "abcde");

功能:

protected static WindowsDriver<WindowsElement> desktopSession;

public static void waitForElementName(long timeout, string elementName)
{
    WebDriverWait wait = new WebDriverWait(desktopSession, new TimeSpan(timeout));
    wait.Until(ExpectedConditions.ElementIsVisible(OpenQA.Selenium.By.Name(elementName)));     
} 

在这种情况下,我想为元素等待 120 秒。但总是在 40~60 秒内,超时中断,调试器带有以下消息:

消息:测试方法抛出异常: System.InvalidOperationException:无法定位元素 使用给定搜索参数的页面。

谢谢!

【问题讨论】:

    标签: c# selenium-webdriver uwp


    【解决方案1】:

    这对我有用,例如,您必须将 WindowsDriver 以及使用 'TimeSpan.FromSeconds(timeout)' 设置的超时一起传递给 WebDriverWait。

    protected static WindowsDriver<WindowsElement> desktopSession;
    
    public static void waitForElementName(long timeout, string elementName)
    {
        WebDriverWait wait = new WebDriverWait(desktopSession,TimeSpan.FromSeconds(timeout));
        wait.Until(ExpectedConditions.ElementIsVisible(OpenQA.Selenium.By.Name(elementName)));     
    } 
    

    我希望这会有所帮助。

    谢谢

    【讨论】:

    • 我不明白你的建议。如何将驱动程序传递给 WebDriverWait?可以给我写一个代码示例吗?谢谢!
    • 您好,我已经提供了一个示例作为我的回答的一部分。基本上,您正在传递新的 TimeSpan(timeout)。这应该是 TimeSpan.FromSeconds(timeout) 所以它应该看起来像这样 .WebDriverWait(desktopSession,TimeSpan.FromSeconds(timeout));我希望这有帮助。我很乐意为任何其他问题提供帮助如果它确实有帮助,请将答案标记为正确。谢谢
    【解决方案2】:

    我使用 2 个不同的驱动程序并更改 timeoutImplictlyWait() 值来解决我的问题。

    1 - 为了访问 windows 元素,我声明了没有超时参数的驱动程序(如果你不声明它,默认超时为 60 秒)并将 ImplictlyWait() 设置为 10 秒:

    protected static WindowsDriver<WindowsElement> desktopSession;
    
    desktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities);
    
    desktopSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    

    2 - 对于 WaitFor() 方法,我声明驱动程序传递超时和 ImplictlyWait 值,如以下代码:

    public static void WaitForElementByName(TimeSpan timeout, string elementName)
    {
        WindowsDriver<WindowsElement> driver = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities, timeout);
    
        driver.Manage().Timeouts().ImplicitlyWait(timeout);
    
        WebDriverWait wait = new WebDriverWait(driver, timeout);
        wait.Until(ExpectedConditions.ElementIsVisible(OpenQA.Selenium.By.Name(elementName)));
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2020-04-04
      • 2015-09-14
      • 1970-01-01
      • 2020-05-02
      • 2020-07-18
      • 2018-03-30
      • 1970-01-01
      • 2013-03-24
      相关资源
      最近更新 更多