Terry Chen
Terry Chen

Reputation: 133

Selenium Node API web driver wait timeout handler

I am new to Selenium web drive. Trying to do some page automation and are using driver.wait functions to wait for a selector rendering first and then do some operations later.

Was wondering if Selenium has a way to pass in a timeout handler to manage timeout if the element is not showing up after x seconds.

Here's my code:

driver.wait(function () {
    return driver.isElementPresent(webdriver.By.css('input[id="searchMap"]'));
}, 10000);

So after 10 secs if input[id="searchMap"] does not show up, Selenium script will end and Error is thrown.

I am looking for something like this:

driver.wait(function () {
    return driver.isElementPresent(webdriver.By.css('input[id="searchMap"]'));
}, 10000, function fail(){
    console.log("Time is up!");
});

Upvotes: 0

Views: 1280

Answers (1)

Terry Chen
Terry Chen

Reputation: 133

Found a solution myself. Have to use catch for Selenium promise class.

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise_exports_Promise.html

Here is my code:

driver.wait(function () {
    return driver.isElementPresent(webdriver.By.css('div.info-page'));
}, 10000).catch(function(e){
    console.log('Catching Error');
});

Upvotes: 1

Related Questions