";无法定位元素“”;没有睡眠指令时出错


"Unable to locate element" error without sleep instruction

我有以下硒测试:

class MyTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public function testFunction() {
        $this->url('https://mywebsiteurl.com');
        // ...
        // Several selenium actions here
        // ...
        $this->byId('form-submit-button')->click(); // Submit a form to another URL
        $this->byId('next')->click();
    }
}

随机地,我的测试失败了,出现了以下错误:

PHPUnit_Extensions_Selenium2TestCase_WebDriverException: Unable to locate element: {"method":"id","selector":"next"}
Command duration or timeout: 49 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28'
System info: host: 'selenium-server', ip: '15.121.50.61', os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-4-amd64', java.version: '1.6.0_27'
Session ID: 4e7822e4-73dc-473e-8010-6717a04d243e
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=24.6.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]

当我看到生成的屏幕截图时,我可以看到我的网页上有"下一个"按钮,当我在调用"点击"方法之前添加"睡眠(5)"指令时,我的测试每次都通过了。

你知道如何解决我的问题吗?

Ben

我是个java人。。但是,根据你的问题,我建议你要么隐性等待,要么显性等待,以解决你的问题。。

在谷歌上,我发现了这段隐式等待代码,你可以在这里使用它来解决你的问题:

$this->timeouts()->implicitWait(30000); 

将其添加到$this->url('https://mywebsiteurl.com');上方的代码顶部。这将确保,每次硒试图定位元素时,它将等待最多30秒希望这能有所帮助。