如何计算与 CSS 选择器匹配的元素数量?
我正在尝试使用 SeleniumRC 来测试我的 GWT 应用程序,并尝试使用 CSS 选择器来匹配元素。
我想计算以下 HTML 中启用的按钮的数量。
如果按钮位于带有 class="x-panel-btn-td "
的 下,则启用按钮;如果按钮位于
< 下,则禁用按钮;td>
与 class="x-panel-btn-td x-hide-offsets"
。
所以基本上,我想检索带有 x-panel-btn-td
类的所有 下的按钮数量。
<table cellspacing="0">
<tbody>
<tr>
<td id="ext-gen3504" class="x-panel-btn-td ">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">OK</button>
</em>
</td>
<td id="ext-gen3512" class="x-panel-btn-td x-hide-offsets">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">Yes</button>
</em>
</td>
<td id="ext-gen3520" class="x-panel-btn-td">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">No</button>
</em>
</td>
<td id="ext-gen3528" class="x-panel-btn-td x-hide-offsets">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">Cancel</button>
</em>
</td>
</tr>
</tbody>
</table>
I am trying to use SeleniumRC to test my GWT App and am trying to match elements using CSS selectors.
I want to count the number of enabled buttons in the following HTML.
A button is enabled if it is under a <td>
with class="x-panel-btn-td "
and disabled if it is under a <td>
with class="x-panel-btn-td x-hide-offsets"
.
So basically, I want to retrieve the number of buttons under all <td>
s with the class x-panel-btn-td
.
<table cellspacing="0">
<tbody>
<tr>
<td id="ext-gen3504" class="x-panel-btn-td ">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">OK</button>
</em>
</td>
<td id="ext-gen3512" class="x-panel-btn-td x-hide-offsets">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">Yes</button>
</em>
</td>
<td id="ext-gen3520" class="x-panel-btn-td">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">No</button>
</em>
</td>
<td id="ext-gen3528" class="x-panel-btn-td x-hide-offsets">
<em unselectable="on">
<button id="ext-gen3506" class="x-btn-text" type="button">Cancel</button>
</em>
</td>
</tr>
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
据我所知,您无法使用 CSS 选择器执行此操作,但 Selenium 中有一个用于通过 XPath 进行计数的命令。以下命令将验证是否有两个禁用按钮:
在 Selenium RC (Java) 中,这看起来更像是
As far as I am aware you can't do this using CSS selectors, but there is a command in Selenium for counting by XPath. The following command will verify there are two disabled buttons:
In Selenium RC (Java) this would look more like
现在,这也在 Selenium Webdriver API 由于 google 仍然链接到此问题作为最佳结果,即使 Selenium RC 已被 Webdriver 取代,希望这可以节省某人的时间。
java代码示例:
This is now also implemented (without any extra Javascript magic needed) in Selenium Webdriver API Since google still links to this question as a top result, even though Selenium RC has been replaced by Webdriver, hopefully this saves someone time.
Example java code:
在较新版本的 Selenium 中,有一个函数 GetCSSCount(string locator)。只是觉得这个问题的更新会有用
With newer versions of Selenium, there is a function GetCSSCount(string locator). Just thought an update to this question would be useful
由于 Selenium 是 Firefox 的一部分,并且后者支持 Selectors API,因此可以使用如下测试来简化 CSS 定位器匹配的计数:
当然,在此示例中,计数被验证为 4。
Since Selenium is part of Firefox and the latter is supporting Selectors API one could simplify counting matches of a CSS locator using a test like this:
In this example count is verified to be 4, of course.
这个应该比较简单。您可以通过多种方式完成此操作,但我建议使用 DefaultSelenium 中的
getEval(...)
。编写一些 JavaScript:
一般来说,
getEval(...)
将返回最后运行的语句的值...,这样您就可以得到计数。This should be relatively simple. You can do it multiple ways but I would suggest using the
getEval(...)
in DefaultSelenium.Write some JavaScript that:
Generally,
getEval(...)
will return the value of the last statement that ran... so that should give you the count.这是另一个使用 javascript 的解决方案,类似于有关 Selector API / window.document.querySelectorAll 的帖子:
http://blog.eviltester.com/2010/03/a-simple-getcsscount-helper-method-for-use-with-selenium-rc .html
Here's another solution, using javascript, similar to post about Selector API / window.document.querySelectorAll:
http://blog.eviltester.com/2010/03/a-simple-getcsscount-helper-method-for-use-with-selenium-rc.html