Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - How to Wait?

I need to be able to wait for one of multiple things that could happen to a page after a certain action is taken. Examples of these things are: URL changes, a certain title is set, certain content on the page appears, etc.

This explains how to wait - https://github.com/facebook/php-webdriver/wiki/HowTo-Wait. However, I need to be able to wait for multiple things at the same time. I want the waiting to stop once one of the conditions occur.

Is there a way to "or" during a wait (e.g. wait for URL to change OR title contains "foo" OR "bar" appears on the page, etc.)?

like image 779
StackOverflowNewbie Avatar asked Jun 17 '17 13:06

StackOverflowNewbie


People also ask

Why do we need wait time in selenium?

It is usually necessary to introduce a wait time in the Selenium script when navigating Ajax-based or dynamic elements that may continue to load after the page loads. Certain elements may only become visible after the page loads or after a user action but be available for interaction after a few seconds have passed.

How do you wait for a page to load in selenium?

There are three ways to implement Selenium wait for page to load: The Implicit Wait tells WebDriver to wait a specific amount of time (say, 30 seconds) before proceeding with the next step. If the tester knows how much time the page and element will take to load, they should use Implicit Wait.

What is WAIT command in Selenium WebDriver?

Selenium Wait commands instruct a test to pause for a predetermined length of time before moving onto the next step in the script. The pause lets the page load and the web elements become visible/present/populated/clickable before WebDriver can interact with them and proceed with the test.

What is implicit wait in selenium?

Implicit Wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs. Once the command is in place, Implicit Wait stays in place for the entire duration for which the browser is open.


1 Answers

In the same link you posted, look for the "Custom conditions" section, i.e.:

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

        return count($elements) > 5;
    },
    'Error locating more than five elements'
);

Note the use of "findElements" in the code example. When nothing is found, an empty array will be returned. If only one of three elements must be visible, you do something like:

$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'
);
like image 78
misterjake Avatar answered Oct 07 '22 19:10

misterjake