C# 等待 Selenium Webdriver 更改的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10426321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice to wait for a change with Selenium Webdriver?
提问by Kir
After a click event I need to wait for an elements attribute to change before proceeding further (click event causes certain elements to move out of focus and certain others get focus via a JS)
单击事件后,我需要等待元素属性发生更改,然后再继续(单击事件会导致某些元素移出焦点,而某些其他元素通过 JS 获得焦点)
After spending time searching for a reliable alternative to "waitForAttribute" (selenium 1 command) in web driver... I could get the below code to work. But I am not sure if this is the best implementation...
在花费时间在 Web 驱动程序中搜索“waitForAttribute”(selenium 1 命令)的可靠替代方案之后......我可以让下面的代码工作。但我不确定这是否是最好的实现......
Any other better solution??
还有其他更好的解决方案吗??
wait = new WebDriverWait(wedriver1, TimeSpan.FromSeconds(5));
.....
button.Click();
wait.Until(webdriver1 => webdriver2.webelement.GetAttribute("style").Contains("display: block"));
Also, can anyone please share a link to how I can handle AJAX event changes using webdriver.
另外,任何人都可以分享我如何使用 webdriver 处理 AJAX 事件更改的链接。
采纳答案by Silver Quettier
It's a recurring problem of Selenium. I am not sure about a "best" implementation existing. I believe it's largely depending on the situation.
这是硒的一个反复出现的问题。我不确定是否存在“最佳”实现。我认为这在很大程度上取决于情况。
Concerning AJAX-driven changes, I would generally use a waitForElementPresent or a waitForElementNotPresent depending on the changes the AJAX call will make on the page.
关于 AJAX 驱动的更改,我通常会使用 waitForElementPresent 或 waitForElementNotPresent,具体取决于 AJAX 调用将在页面上进行的更改。
回答by John
You could use Thread.sleep like people have implemented here..You can use their implementations to relate to your problem.
您可以像人们在这里实现的那样使用 Thread.sleep 。.您可以使用他们的实现来解决您的问题。
public void waitFor(Webdriver driver,,WebElement welElem, String attributeValue){
int timeout=0;
while(true){
if(driver.webElem.getAttribute(attribute)!=null){
break;
}
if(timeout>=10){
break;
}
Thread.sleep(100);
timeout++;
}
}
I think you could implement something like this. I have not checked it but you get the idea.
我认为你可以实现这样的东西。我没有检查过,但你明白了。
回答by Manpreet Singh
You can use implicit WebDriver wait :
您可以使用隐式 WebDriver 等待:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用)时轮询 DOM 一段时间。
More here: http://seleniumhq.org/docs/04_webdriver_advanced.html#implicit-waits
更多信息:http: //seleniumhq.org/docs/04_webdriver_advanced.html#implicit-waits
回答by Hari Reddy
I feel that using the WebDriverWait is pretty useful. One change you can make in your code is use -
我觉得使用 WebDriverWait 非常有用。您可以在代码中进行的一项更改是使用 -
webelement.isDisplayed();
instead of getting the style attribute.
而不是获取样式属性。
回答by jersey-city-ninja
I suggest use org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe(WebElement element, String attribute, String value).
我建议使用org.openqa.selenium.support.ui.ExpectedConditions.attributeToBe(WebElement element, String attribute, String value).
e.g.
例如
WebDriverWait wait = new WebDriverWait(driver, 5); // time out after 5 seconds
someElement.click();
wait.until(ExpectedConditions.attributeToBe(someElement, "sort-attribute", "ascending"));
(docs)
(文档)

