Mink/Goutte How to check checkbox without attribute in Goutte?

牧云@^-^@ 提交于 2019-12-18 09:48:22

问题


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:


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



来源:https://stackoverflow.com/questions/37277333/mink-goutte-how-to-check-checkbox-without-attribute-in-goutte

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!