硒-如何等待?

Sta*_*bie 3 selenium php-webdriver

我需要能够在采取某些措施之后等待页面可能发生的多种情况之一。这些示例包括:URL更改,设置了特定标题,页面上出现了某些内容,等等。

这说明了如何等待-https: //github.com/facebook/php-webdriver/wiki/HowTo-Wait。但是,我需要能够同时等待多件事。我希望在其中一种情况发生时停止等待。

是否有一种在等待期间进行“或”操作的方法(例如,等待URL更改标题包含“ foo” 页面上出现“栏”等)?

mis*_*ake 5

在您发布的同一链接中,查找“自定义条件”部分,即:

$driver->wait()->until(
    function () use ($driver) {
        $elements = $driver->findElements(WebDriverBy::cssSelector('li.foo'));

        return count($elements) > 5;
    },
    'Error locating more than five elements'
);
Run Code Online (Sandbox Code Playgroud)

注意在代码示例中使用“ findElements”。如果什么也没找到,将返回一个空数组。如果只有三个元素之一必须可见,则可以执行以下操作:

$driver->wait()->until(
    function () use ($driver) {
        $elements1 = $driver->findElements(WebDriverBy::cssSelector('li.foo1'));
        $elements2 = $driver->findElements(WebDriverBy::cssSelector('li.foo2'));
        $elements3 = $driver->findElements(WebDriverBy::cssSelector('li.foo3'));

        return count($elements1) + count($elements2) + count($elements3) > 0;
    },
    'Error locating at least one of the elements'
);
Run Code Online (Sandbox Code Playgroud)