java 如何以及何时实现 Selenium WebDriver 的刷新(ExpectedCondition<T> 条件)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28026845/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:52:29  来源:igfitidea点击:

How and when to implement refreshed(ExpectedCondition<T> condition) of Selenium WebDriver?

javaseleniumselenium-webdriver

提问by TDHM

I was going through methods of ExpectedCondtionsclass and found one method: refreshed

我正在浏览ExpectedCondtions类的方法并找到一种方法:刷新

I can understand that the method can be used when you get StaleElementReferenceExceptionand you want to retrieve that element again and this way to avoid StaleElementReferenceException

我可以理解,当您获得StaleElementReferenceException并且您想再次检索该元素时可以使用该方法,这样可以避免StaleElementReferenceException

My above understanding might not be correct hence I want to confirm:

我的上述理解可能不正确,因此我想确认:

  1. When refreshedshould be used?
  2. What should be the code for somethingpart of following code:
  1. refreshed应使用?
  2. something以下代码的一部分应该是什么代码:

wait.until(ExpectedConditions.refreshed(**something**));

wait.until(ExpectedConditions.refreshed(**something**));

Can someone please explain this with an example?

有人可以用一个例子来解释这个吗?

采纳答案by ddavison

According to the source:

根据消息来源:

Wrapper for a condition, which allows for elements to update by redrawing. This works around the problem of conditions which have two parts: find an element and then check for some condition on it. For these conditions it is possible that an element is located and then subsequently it is redrawn on the client. When this happens a {@link StaleElementReferenceException} is thrown when the second part of the condition is checked.

条件的包装器,允许通过重绘来更新元素。这解决了由两部分组成的条件问题:找到一个元素,然后检查它的某些条件。对于这些情况,可能会定位到一个元素,然后在客户端上重新绘制它。发生这种情况时,会在检查条件的第二部分时抛出 {@link StaleElementReferenceException}。

So basically, this is a method that waits until a DOM manipulation is finished on an object.

所以基本上,这是一个等待对象的 DOM 操作完成的方法。

Typically, when you do driver.findElementthat object represents what the object is.

通常,当您执行driver.findElement该对象时,该对象表示该对象是什么。

When the DOM has manipulated, and say after clicking a button, adds a class to that element. If you try to perform an action on said element, it will throw StaleElementReferenceExceptionsince now the WebElementreturned now does not represent the updated element.

当 DOM 操作完毕后,比如单击按钮后,向该元素添加一个类。如果您尝试对所述元素执行操作,它将抛出,StaleElementReferenceException因为现在WebElement返回的 now 不代表更新的元素。

You'll use refreshedwhen you expect DOM manipulation to occur, and you want to wait until it's done being manipulated in the DOM.

refreshed当您期望 DOM 操作发生时,您将使用,并且您希望等到它在 DOM 中被操作完成。

Example:

例子:

<body>
  <button id="myBtn" class="" onmouseover="this.class = \"hovered\";" />
</body>

// pseudo-code
1. WebElement button = driver.findElement(By.id("myBtn")); // right now, if you read the Class, it will return ""
2. button.hoverOver(); // now the class will be "hovered"
3. wait.until(ExpectedConditions.refreshed(button));
4. button = driver.findElement(By.id("myBtn")); // by this point, the DOM manipulation should have finished since we used refreshed.
5. button.getClass();  // will now == "hovered"

Note that if you perform say a button.click()at line #3, it will throw a StaleReferenceException since the DOM has been manipulated at this point.

请注意,如果您button.click()在第 3 行执行 say a ,它将抛出 StaleReferenceException,因为此时 DOM 已被操作。

In my years of using Selenium, I've never had to use this condition, so I believe that it is an "edge case" situation, that you most likely won't even have to worry about using. Hope this helps!

在我使用 Selenium 的这些年里,我从来没有使用过这种情况,所以我相信这是一个“边缘情况”的情况,你很可能甚至不必担心使用。希望这可以帮助!

回答by HaC

The refreshedmethod has been very helpful for me when trying to access a search result that has been newly refreshed. Trying to wait on the search result by just ExpectedConditions.elementToBeClickable(...)returns StaleElementReferenceException. To work around that, this is the helper method that would wait and retry for a max of 30s for the search element to be refreshed and clickable.

refreshed当我尝试访问新刷新的搜索结果时,该方法对我非常有帮助。试图通过只ExpectedConditions.elementToBeClickable(...)返回来等待搜索结果StaleElementReferenceException。为了解决这个问题,这是一个辅助方法,它会等待并重试最多 30 秒,以便搜索元素刷新和可点击。

public WebElement waitForElementToBeRefreshedAndClickable(WebDriver driver, By by) {
    return new WebDriverWait(driver, 30)
            .until(ExpectedConditions.refreshed(
                    ExpectedConditions.elementToBeClickable(by)));
}

Then to click on the result after searching:

然后点击搜索后的结果:

waitForElementToBeRefreshedAndClickable(driver, By.cssSelector("css_selector_to_search_result_link")).click();

Hope this was helpful for others.

希望这对其他人有帮助。