【问题标题】:How do I get Selenium to wait for a page to load fully before executing the click() command如何让 Selenium 在执行 click() 命令之前等待页面完全加载
【发布时间】:2017-09-18 14:44:47
【问题描述】:

我的问题: 我正在使用 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) 处不可点击。其他元素将收到点击:(会话信息:chrome=57.0.2987.133)(驱动程序信息:chromedriver=2.29.461591(62ebf098771772160f391d75e589dc567915b233)

我要做的是等待 DOM 完全加载,然后再单击按钮。但是我间歇性地收到上述错误。有谁知道解决这个问题的方法??快把我逼疯了!!

【问题讨论】:

标签: selenium-webdriver phpunit selenium-chromedriver


【解决方案1】:

尝试显式等待。 “显式等待是您定义的代码,用于在继续执行代码之前等待某个条件发生。提供了一些便利方法,可帮助您编写仅在需要时等待的代码。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)。

【讨论】:

  • @GhostCat 已修复。
【解决方案2】:

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

这是怎么写的

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

对不起,我不知道如何用 PHP 编写它

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 2020-02-08
    • 2016-09-14
    • 2014-01-16
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多