在执行click()命令之前,如何让Selenium等待页面完全加载

时间:2017-04-21 14:34:52

标签: selenium-webdriver phpunit selenium-chromedriver

我的问题: 我正在使用Selenium运行phpunit来测试位于世界另一端的服务器上的网站。因此,点击标签页或新页面会延迟几秒钟。我用Chromedriver启动Selenium Server。 例如

    public function setUp()
    {
        $this->setHost('localhost'); // Set the hostname for the connection to the Selenium server.
        $this->setPort(4444); // set port # for connection to selenium server
        $this->setBrowser('chrome'); // set the browser to be used
        $this->setBrowserUrl('https://www.*.com');  // set base URL for tests
        $this->prepareSession()->currentWindow()->maximize(); // Maximize the window when the test starts 
        $this->timeouts()->implicitWait(30000); // Wait up to 30 seconds for all elements to appear
     }

    public function testLoginToeSeaCare(){
        $this->timeouts()->implicitWait(10000); // Wait up to 10 seconds for all elements to appear

        $url = 'https://www.*.com';
        $loginName = 'Ned';
        $loginPassword = 'Flanders';

        $this->url($url); // Load this url

        $this->timeouts()->implicitWait(30000); // Wait up to 30 seconds for all elements to appear
        $username = $this->byId('username'); // Search page for input that has an id = 'username' and assign it to $username
        $password = $this->byId('password'); // Search page for input that has an id = 'password' and assign it to $password

        $this->byId('username')->value($loginName); // Enter the $loginName text in username field
        $this->byId('password')->value($loginPassword); // Enter the $loginPassword in password field
        $this->byCssSelector('form')->submit(); // submit the form


        $tab1Link = $this->byLinkText("Tab1"); // Search for the textlink Tab1
        $this->assertEquals('Tab1', $tab1Link->text()); // assert tab text is present

        $this->timeouts()->implicitWait(10000); // Wait up to 10 seconds for all elements to appear
        $tab2Link = $this->byLinkText("Tab2");
        $tab2Link->click(); // Click 'Tab2' tab
    }

运行上述内容时报告错误,我将其捕获到xml文件中: ******** :: testSearch PHPUnit_Extensions_Selenium2TestCase_WebDriverException:未知错误:元素...在点(430,139)处无法点击。其他元素将收到点击:(会话信息:铬= 57.0.2987.133)(驱动信息:chromedriver = 2.29.461591(62ebf098771772160f391d75e589dc567915b233)

我要做的是在点击按钮之前等待DOM完全加载。但我间歇性地得到了上述错误。有没有人知道这个方法?它让我疯了!!

2 个答案:

答案 0 :(得分:2)

尝试显式等待。 “显式等待是您定义的代码,用于在进一步执行代码之前等待某个条件发生。有一些方便的方法可以帮助您编写只需要等待的代码.WebDriverWait与ExpectedCondition结合使用一种可以实现的方式。“

例如,

// Wait for the page title to be 'My Page'. 

// Default wait (= 30 sec)
$driver->wait()->until(WebDriverExpectedCondition::titleIs('My Page'));


// Wait for at most 10s and retry every 500ms if it the title is not     correct.
$driver->wait(10, 500)->until(WebDriverExpectedCondition::titleIs('My Page'));

您可以将许多准备好的条件传递给until()方法。所有这些都是WebDriverExpectedCondition的子类,包括elementToBeClickable()(https://github.com/facebook/php-webdriver/wiki/HowTo-Wait)。

答案 1 :(得分:0)

我不知道这是否会对你有所帮助 但是在java中有一种等待某个项目可见的方法

以下是它的编写方式

WebDriverWait Wait=new WebDriverWait(Driver, 10);
Wait.until(ExpectedConditions.visibilityOf(Driver.findElement(By.//your element locator)));

抱歉,我不知道如何用PHP编写