如何以及何时实施Selenium WebDriver的刷新(ExpectedCondition T条件)? [英] How and when to implement refreshed(ExpectedCondition<T> condition) of Selenium WebDriver?

查看:195
本文介绍了如何以及何时实施Selenium WebDriver的刷新(ExpectedCondition T条件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历ExpectedCondtions类的方法,发现了一个方法:

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

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

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

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

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

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

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

有人可以举例说明吗?

推荐答案

根据消息来源:

一个条件的包装器,它允许元素通过重绘来更新. 这可以解决条件问题,其中包括两个部分:找到一个 元素,然后检查其上是否存在某些情况.对于这些条件 可能找到一个元素,然后在其上重新绘制它 客户端.发生这种情况时,{@ link StaleElementReferenceException}是 条件的第二部分被检查时抛出.

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.

因此,基本上,这是一种等待对对象完成DOM操作的方法.

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

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

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

操纵DOM后,并单击按钮后说,将一个类添加到该元素.如果您尝试对上述元素执行操作,则它将抛出StaleElementReferenceException,因为现在返回的WebElement并不代表更新后的元素.

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 StaleElementReferenceException since now the WebElement returned now does not represent the updated element.

当您希望进行DOM操作时,您将使用refreshed,而您要等到在DOM中完成对它的操作为止.

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

<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"

请注意,如果您在第3行执行button.click(),由于此时已对DOM进行了操作,它将抛出StaleReferenceException.

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.

在我使用硒的那些年里,我从来没有使用过这种条件,因此我认为这是一个边缘案例".在这种情况下,您很可能甚至不必担心使用.希望这会有所帮助!

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 WebDriver的刷新(ExpectedCondition T条件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆