使用 Selenium 确定是否选中特定类的复选框

发布于 2024-09-25 23:11:11 字数 446 浏览 1 评论 0原文

我正在使用 Selenium 和 PHPUnit 来尝试判断是否选中了一堆具有特定类的复选框,但我遇到了一些麻烦。

我的代码:

$count = $this->getXpathCount('//form//input[@type="checkbox" and @class="required"]');
for ($i = 1; $i <= $count; $i++) {
    $this->assertTrue($this->isChecked(sprintf('xpath=//form//input[@type="checkbox" and @class="required"][%d]', $i)));
}

不幸的是,我似乎不能在同一个标​​签上使用方括号两次,但我确实需要确保所有具有“required”类的复选框都被选中。

有什么建议吗?

I'm using Selenium with PHPUnit to try to tell whether a bunch of checkboxes with a specific class are checked and I'm having a bit of trouble.

My code:

$count = $this->getXpathCount('//form//input[@type="checkbox" and @class="required"]');
for ($i = 1; $i <= $count; $i++) {
    $this->assertTrue($this->isChecked(sprintf('xpath=//form//input[@type="checkbox" and @class="required"][%d]', $i)));
}

Unfortunately, it doesn't seem like I can use square brackets twice on the same tag, but I do need to make sure all checkboxes that have the class "required" are checked.

Any Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我一直都在从未离去 2024-10-02 23:11:11

我不了解 Selenium,但是 DOMXPath->evaluate 会理解这种语法&返回一个浮点数(不是 int,但是嘿),也许它适合你:

count(//form//input[@type="checkbox" and @class="required" and not(@checked)])

或者也许是一个简单的:

$this->assertTrue($this->getXpathCount('//form//input[@type="checkbox" and @class="required" and not(@checked)]')==0);

I don't know about Selenium, but a DOMXPath->evaluate would understand this syntax & return a float (not int, but hey), maybe it works for you:

count(//form//input[@type="checkbox" and @class="required" and not(@checked)])

Or perhaps a simple:

$this->assertTrue($this->getXpathCount('//form//input[@type="checkbox" and @class="required" and not(@checked)]')==0);
凉栀 2024-10-02 23:11:11

除非我误会,否则两次使用方括号应该不会有任何问题。我能够使用 PHPUnit 和 Selenium 针对下面的 html 让您的代码顺利运行:

<html>
  <head>
    <title>Check Boxes</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="self" method="post">
        <div>
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="submit" value="Submit" />
            <br />
        </div>
    </form>
  </body>
</html>

您是否正在运行最新版本的 PHPUnit 和 Selenium-rc?

Unless I'm misunderstanding, you shouldn't have any problems using square brackets twice. I was able to get your code to work without trouble using PHPUnit and Selenium against the html below:

<html>
  <head>
    <title>Check Boxes</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="self" method="post">
        <div>
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="checkbox" class="required"/>
            <br />
            <input type="submit" value="Submit" />
            <br />
        </div>
    </form>
  </body>
</html>

Are you running the most recent version of PHPUnit and Selenium-rc?

飘然心甜 2024-10-02 23:11:11

$checkbox3 = $this->byClassName('required');
$this->assertTrue($checkbox3->selected());

$checkbox3 = $this->byClassName('required');
$this->assertTrue($checkbox3->selected());

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