Watir webdriver;计数具有更改类名称的元素 [英] Watir webdriver; counting elements with changing class names

查看:110
本文介绍了Watir webdriver;计数具有更改类名称的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rspec和Watir-webdriver,并且希望获得点击的所有元素的计数。

I am using Rspec and Watir-webdriver and am looking to get a count of all the elements that have been clicked.

我在做什么:

我执行搜索并返回结果列表;然后我运行一个循环,只选择其中的5。这些元素的类名从资产卡选择到现在说资产卡可选择

I perform a search and get back a list of results; I then run a loop to select only 5 of them. The class name for these elements goes from asset-card selectable to now saying asset-card selectable selected.

我尝试通过这样做正确计数:

I try to get a proper count by doing this:

count = @browser.elements(:class, 'selected').size

> all 结果,而不是现在已经选择 作为类名的一部分。

But this is still giving me all results and not specifically the ones that now have selected as part of the class name.

任何想法如何我可以完成这个?
这是html的样子:

Any thoughts on how I can accomplish this?
This is what the html looks like:

<div class="search results">
    <div class="asset-card selectable selected"></div>
    <div class="asset-card selectable selected"></div>
    <div class="asset-card selectable"></div>
    <div class="asset-card selectable"></div>
    <div class="asset-card selectable"></div>
</div>

在此HTML示例中,选择了前两个元素

In this html example, the first two elements have been selected

推荐答案

我感觉很蠢;我找到一个解决方案,修正为什么我没有得到一个正确的计数返回:这是因为我不等待足够长的时间



在我放置一个 sleep 1 时间后,它返回一个计数准确的我点击。我的想法不完全是技术,但它的行为好像点击操作和类名更改执行操作之前,可以确定正确的计数。
我会寻找一个更优雅的等待解决方案,因为我想避免不必使用 sleep 命令(如果可能)

I feel rather dumb; I found a solution that fixes the issue of why I wasn't getting a proper count returned: it's because I wasn't waiting long enough

After I placed a sleep 1 duration, it returned a count accurate to what I was clicking on. My thought on this isn't exactly technical, but it was acting as if the click action and class name change was performing actions before a proper count could be determined.
I will be looking to place a more elegant wait solution as I'd like to avoid having to use sleep commands if possible

这篇关于Watir webdriver;计数具有更改类名称的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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