【问题标题】:Mink/Goutte How to check checkbox without attribute in Goutte?Mink/Goutte 如何在 Goutte 中选中没有属性的复选框?
【发布时间】:2019-12-28 00:42:36
【问题描述】:

我提前道歉,但我是初学者。 我尝试检查没有 id 或 name 的复选框。

<span class="ps-align-left">
<input type="checkbox" value="43899" style="background-color: rgb(252, 252, 252);"/>
                                            43899
</span>

我知道如何用 selenium2driver 来做这件事。所以我像这样使用“find”函数:

public function checkOption()
{
    $this->getSession()->getPage()->find('css', '.ps-align-left>input')->check();
}

它工作正常,但是当我尝试使用无头浏览器 Goutte 运行测试时出现错误:

/usr/bin/php5.6 /tmp/ide-behat.php --format PhpStormBehatFormatter /home/grzegorz/PhpstormProjects/Test/features/scenariusze.feature
Testing started at 14:48 ...

Malformed field path ""

谁能知道原因?我应该使用不同的功能吗?

【问题讨论】:

    标签: behat mink goutte


    【解决方案1】:

    尝试使用 click 并添加 Exception 以防找不到元素。

    示例: 如果未找到该元素,find 方法将返回 null,您将尝试单击 null,这将引发致命异常并且您的套件将停止。

    如果您添加异常,则只有当前场景会失败,套件将继续执行。

    public function iClick($selector, $locator){
        $node = $this->getSession()->getPage()->find($selector, $locator);
    
        if($node === null){
            throw new Exception("Element $locator not found!");
        }else{
            $node->click();
        }
    }
    

    如果元素是checkbox类型,你想做一个check,不管是否被check,你都可以创建一个使用check()而不是click()的方法

    【讨论】:

      【解决方案2】:

      您必须使用 MinkContext 的 checkOption($option) 方法(在 Behat/MinkExtension 中,您必须通过 Composer 安装)。

      MinkContext 默认安装在 Behat 环境中。

      MinkContext.php

      希望它有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-17
        • 2014-11-24
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 2017-10-13
        相关资源
        最近更新 更多