Python Selenium:等到元素不再陈旧? [英] Python Selenium: wait until an element is no longer stale?

查看:314
本文介绍了Python Selenium:等到元素不再陈旧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我想等到元素不再处于STALE状态,即直到元素连接到DOM.以下等待选项无法正常工作:

I have a situation in which I want to wait until an element is no longer STALE i.e. until an elements gets connected to the DOM. Following wait options do not work somehow:

self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
self.wait.until(EC.presence_of_element_located((By.ID, "elementID")))

存在相反的等待功能,该功能一直等待到元素变旧为止,即:

Its opposite wait function is present which waits until an element becomes stale, which is:

self.wait.until(EC.staleness_of((By.ID, "elementID")))

但是我希望它等到元素不再存在,即直到它连接到DOM.如何实现此功能?

But I want it to wait until the element is NO LONGER STALE i.e. until it gets connected to the DOM. How can I achieve this functionality?

这里有一个解决方案:这里,但我正在寻找任何其他更好的方法.

there is a solution here : here but I am looking for any other better approach if any.

推荐答案

陈旧的元素是您存储的元素引用,由于页面,页面的一部分甚至只是刷新了元素,该引用不再有效.一个简单的例子

A stale element is an element reference that you have stored that is no longer valid because the page, part of the page, or maybe just the element was refreshed. A simple example

element = driver.find_element_by_id("elementID")
# do something that refreshes the page
element.click()

此处element.click()将引发陈旧的元素异常,因为该引用在刷新之前存储在 中,但在刷新后使用(单击了).在这种情况下,该引用不再有效.一旦引用过时,它就永远不会变得过时"……该引用将永远无法再次使用.解决该问题的唯一方法是再次存储引用.

Here element.click() will throw a stale element exception because the reference was stored before the refresh but used (clicked) after the refresh. In this case, that reference is no longer valid. Once a reference is stale, it never becomes "unstale"... that reference can never be used again. The only way to fix that is to store the reference again.

注意:您的代码示例不适用于.staleness_of().它需要一个Web元素引用,而不是一个定位器.您需要一个现有引用来等待它过时.请参阅

NOTE: Your code sample is not correct for .staleness_of(). It takes a web element reference, not a locator. You need an existing reference to wait for it to go stale. See the docs.

现在要解决问题...您需要等待刷新完成,然后然后获得新的引用.

Now to solve the problem... you need to wait for the refresh to complete and then get a new reference.

element = driver.find_element_by_id("elementID")
# do something that refreshes the element
self.wait.until(EC.staleness_of(element))
element = self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
# do something with element

等待元素变旧等待元素引用丢失,这意味着元素已更改/刷新.知道元素已更改后,我们现在可以对其进行新引用.在这种情况下,等待元素变得可见.现在,我们在变量中存储了一个新引用,可以在没有陈旧元素异常的情况下使用它.

Waiting for the element to become stale waits for the element reference to be lost, which means the element has changed/refreshed. Once we know the element has changed, we can now get a new reference to it. In this case, waiting for the element to become visible. We now have a new reference stored in our variable that we can use without stale element exceptions.

这篇关于Python Selenium:等到元素不再陈旧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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