Mink/Goutte How to check checkbox without attribut

2019-01-29 07:34发布

I apologize in advance but I am very beginner. I try to check checkbox without id or name.

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

I figure out how to do it with selenium2driver. So I use function "find" like this:

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

And it works fine but when I try to run test with headless browser Goutte I get error:

/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 ""

Can anyone knows the reason? Should I use a different function?

1条回答
相关推荐>>
2楼-- · 2019-01-29 08:16

Try to use click and also add Exception in case that the element is not found.

Example: If the element is not found, the find method will return null and you will try to click on null, this will throw a fatal exception and your suite will stop.

if you add an exception only the current scenario will fail and the suite will continue to execute.

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();
    }
}

If the element is type checkbox and you want to do a check, no matter if is checked or not, you can create a method that uses check() instead of click()

查看更多
登录 后发表回答