如何计算与 CSS 选择器匹配的元素数量?

发布于 2024-08-07 09:13:36 字数 1482 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

稀香 2024-08-14 09:13:36

据我所知,您无法使用 CSS 选择器执行此操作,但 Selenium 中有一个用于通过 XPath 进行计数的命令。以下命令将验证是否有两个禁用按钮:

verifyXpathCount | //td[contains(@class, 'x-hide-offsets')]//button | 2

在 Selenium RC (Java) 中,这看起来更像是

assertEquals(selenium.getXpathCount("//td[contains(@class, 'x-hide-offsets')]//button"), 2);

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:

verifyXpathCount | //td[contains(@class, 'x-hide-offsets')]//button | 2

In Selenium RC (Java) this would look more like

assertEquals(selenium.getXpathCount("//td[contains(@class, 'x-hide-offsets')]//button"), 2);
幸福不弃 2024-08-14 09:13:36

现在,这也在 Selenium Webdriver API 由于 google 仍然链接到此问题作为最佳结果,即使 Selenium RC 已被 Webdriver 取代,希望这可以节省某人的时间。

java代码示例:

int locatorElementSize = driver.findElements(By.cssSelector("yourCSSLocator")).size();

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:

int locatorElementSize = driver.findElements(By.cssSelector("yourCSSLocator")).size();
绿萝 2024-08-14 09:13:36

在较新版本的 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

呆橘 2024-08-14 09:13:36

由于 Selenium 是 Firefox 的一部分,并且后者支持 Selectors API,因此可以使用如下测试来简化 CSS 定位器匹配的计数:

verifyEval | window.document.querySelectorAll("your#css > selector.here").length | 4

当然,在此示例中,计数被验证为 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:

verifyEval | window.document.querySelectorAll("your#css > selector.here").length | 4

In this example count is verified to be 4, of course.

抱着落日 2024-08-14 09:13:36

这个应该比较简单。您可以通过多种方式完成此操作,但我建议使用 DefaultSelenium 中的 getEval(...)

编写一些 JavaScript:

  1. 通过 id 获取所有元素:ext-gen3506
  2. 迭代所有元素并检查是否已启用(
  3. 如果已启用),增加计数
  4. “返回”计数。

一般来说,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:

  1. gets all elements by id: ext-gen3506
  2. iterates through all elements and checks to see if it's enabled
  3. if it's enabled, increment a count
  4. "return" the count.

Generally, getEval(...) will return the value of the last statement that ran... so that should give you the count.

惜醉颜 2024-08-14 09:13:36

这是另一个使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文