如何通过 Selenium 和 WebDriverWait 等待元素包含特定属性? [英] How to wait for a element to contain a specific attribute through Selenium and WebDriverWait?

查看:35
本文介绍了如何通过 Selenium 和 WebDriverWait 等待元素包含特定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以提供帮助,我有一个硒问题.我需要进入一个 URL 页面,该页面上的节点最初处于注册"状态,X 秒后,其状态将动态更改为就绪"状态.在它的状态变为就绪"状态之前,我可能会在 selenium 执行期间继续下一步.这里是初始代码的html代码,

<i class = "...">...</i><div class="model-holder"><span class="model-registered">200K</span>

<div class="active-holder">...</div>

这是 X 秒后更新的 html 代码,

<i class = "...">...</i><div class="model-holder"><span class="model-ready">200K</span>

<div class="active-holder">...</div>

我想新建一个 WebDriverWait() Obj 以等待更改发生.这是我的代码:

new WebDriverWait(driver, 50).until(ExpectedConditions.attributeContains(By.xpath("//span[@class = 'model-registered' and text() = '200K']"), "class", "model-ready"));

但是在我的 selenium 运行时,这段代码从不工作,它立即存在.任何想法可能是错误的?提前致谢.

解决方案

要等待元素变为 ready 状态,您需要诱导 WebDriverWait 并且您可以使用以下解决方案:

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//div[@class='model-holder']/span[contains(.,'200K)')]"), "class", "model-ready"));

I have a selenium question if someone could help. I need to get into a URL page where initially a node on that page is in the "registered" status, and X amount of seconds later, its status will dynamically changed to "ready" status. And until its status has moved to "ready' status, I may continue the next steps during selenium execution. Here is the html code of the initial code,

<div class="icon-holder pull-left" action = "select-device" sn="FX0071234" status = "in_stock">
   <i class = "...">...</i>
   <div class="model-holder">
       <span class="model-registered">200K</span>
   </div>
   <div class="active-holder">...</div>
</div>

And here is the updated html code after X amount of seconds,

<div class="icon-holder pull-left" action = "select-device" sn="FX0071234" status = "discovered">
   <i class = "...">...</i>
   <div class="model-holder">
       <span class="model-ready">200K</span>
   </div>
   <div class="active-holder">...</div>
</div>

I want to new a WebDriverWait() Obj to wait for the change happens. Here is my code:

new WebDriverWait(driver, 50).until(ExpectedConditions.attributeContains(
                By.xpath("//span[@class = 'model-registered' and text() = '200K']"), "class", "model-ready"));

But during my selenium run-time, this piece of code never works, it exists instantly. Any ideas what could be wrong ? Thanks in advance.

解决方案

To wait for the element to be changed to ready status you need to induce WebDriverWait and you can use the following solution:

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//div[@class='model-holder']/span[contains(.,'200K')]"), "class", "model-ready"));

这篇关于如何通过 Selenium 和 WebDriverWait 等待元素包含特定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆