首页 > 解决方案 > PHPUnit + Selenium`InvalidArgumentException:找不到元素`

问题描述

遵循此处的示例,尝试实施一些 Selenium 测试。

测试类代码:

class StackoverflowTest extends \PHPUnit_Extensions_Selenium2TestCase
{
    protected function setUp(): void
    {
        $this->setHost('localhost');
        $this->setPort(4444);
        $this->setBrowserUrl('https://stackoverflow.com/');
        $this->setBrowser('chrome');

        parent::setUp();
    }

    public function onNotSuccessfulTest(Throwable $e): void
    {
        $filedata = $this->currentScreenshot();
        $file     = __DIR__ . '/' . time() . '.png';
        file_put_contents($file, $filedata);

        parent::onNotSuccessfulTest($e);
    }

    /**
     * @test
     */
    public function visit_home_page_case_success()
    {
        $this->url('/');
        dump($this->title());
        dd($this->byTag('body')->text()); //line 35
    }
}

控制台输出:

...

"Stack Overflow - Where Developers Learn, Share, & Build Careers"

...

There was 1 error: 1) ...\StackoverflowTest::visit_home_page_case_success InvalidArgumentException: Element not found.

...

.../StackoverflowTest.php:35

...

截屏:

在此处输入图像描述

出了什么问题,我该如何与页面元素进行交互?

在这里重复。

标签: phpseleniumselenium-webdriverphpunit

解决方案


在这里回答:

不幸的是 phpunit-selenium 还不支持 W3C 模式,你可以通过以下方式强制它使用非 w3c 模式:

$this->setDesiredCapabilities(['chromeOptions' => ['w3c' => false]]); 

推荐阅读