2015-04-07 75 views
1

起初:
- 硒2.0 webdriver的
- 为IEXPLORER,Chrome和Firefox
- 当前的webdriver和硒的DLL
- Windows 8.1中
- 的Visual Studio 2013 C#硒的webdriver,等待显示加载的div

我会测试我的网站。这些页面将加载一个Ajax。如果我要更改页面,它将显示一个加载div(div #wartenDialog)。现在我等待显示这个div,然后我切换到下一页。

问题是,有时会有一个小的延迟,直到显示加载div和由一个快速的计算机/互联网没有加载div。

我有尝试这个功能:

public static void WaitWhileElementVisible(RemoteWebDriver _driver, By _locator) 
{ 
    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromMilliseconds(timeout)); 
    wait.Until(drv => !Exists(drv, _locator)); 
} 

private static bool Exists(IWebDriver _drv, By _locator) 
{ 
    return (ExpectedConditions.ElementIsVisible(_locator) != null); 
} 

现在它在超时始终运行。

+0

您还应该显示相关的HTML以及如何使用参数调用'WaitWhileElementVisible'。另外我不明白为什么用参数'IWebDriver _drv'创建定制的'Exists'方法,根本不使用它。 –

+0

_drv是来自其他尝试的剩余物。我已经使用_drv.FindElement(_locator)方法 – Gamer2015

回答

0

等待元素可见的方法是正确的。但是对代码进行一些修改就可以解决问题。

public static void WaitWhileElementVisible(RemoteWebDriver _driver, By _locator) 
{ 
    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromMilliseconds(timeout)); 
    wait.IgnoreExceptionTypes(typeof(WebDriverTimeoutException)); 
    wait.Until(drv => !Exists(drv, _locator)); 
} 

IgnoreExceptionTypes()将忽略提供的异常类型。此代码正在为我工​​作。

希望这可以解决您的问题。

+0

感谢您的快速答案。不幸的是,这并没有改变。我有一个自己的错误:CSS语法必须是div#wartenDialog而不是div #wartenDialog,但我得到了相同的结果。我检测到ExpectedConditions.ElementIsVisible(_locator)!= null方法总是返回true。现在我使用_drv.FindElement(_locator).GetCssValue(“display”)!=“none”表达式并且它可以工作 – Gamer2015

+0

我已经将表达式更改为_drv.FindElement(_locator).Displayed;表达,它也起作用。也许更好的语法 – Gamer2015

+0

@ Gamer2015很高兴听到你解决了这个问题。如果你想忽略使用'WebDriverWait'时发生的异常,你应该使用'IgnoreExceptionTypes'(在你的情况下它是'TimeoutException')。我使用'Displayed'方法遇到了一个问题。如果DOM上不存在元素,则FindElement方法抛出'NoSuchElement'异常。如果元素出现在页面上,它工作正常。大多数情况下,我避免使用“显示”方法。 –