Arcv2
Arcv2

Reputation: 83

How do I catch Selenium Errors using WebDriverJS

So I am using the JavaScript implementation of Selenium, WebDriverJS. As with any web browser automation the biggest hurdle is getting the code slow down long enough for page elements to load. My solution is this:

for each element I want to interact with I have a code block like this

xpath = "//div[@id='gs_lc0']/input[@id='gbqfq']"
driver.wait(function(){
    return waitForElement(xPath,driver);
});
try{
    element = driver.findElement(webdriver.By.xpath(xPath));
}catch(e){
    console.log("Wait Function False Positive")
}
element.sendKeys("Let Me Google That For You\n";

with this as the function repeated in the wait function

var waitForElement = function(path, driver){
    console.log("try: " + path)
    try{
        driver.findElement(webdriver.By.xpath(path));
    }catch (e){
        console.log("FAILURE")
        return false;
    }
    console.log("SUCCESS")
    return true;
}

now this code will work sometimes but othertimes it won't. I suppect the wait function isn't working at all and I'm just getting lucky webpage load times. So to test this theory I added the try function to the code block I cant even get the "NoSuchElementError" to be caught. So the brunt of my question is if there is some other way to form the tryCatch function so these errors will be caught.

Also here is what the head of my code looks like if want a full reproduction

var webdriver = require('selenium-webdriver'), element

var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

driver.get('google.com');

Upvotes: 8

Views: 17717

Answers (3)

vivanov
vivanov

Reputation: 1522

You can also catch that error in a chained "catch" method:

driver.findElement(webdriver.By.id("fofofo")).then(() => {
    // do some task
}).catch((e) => {
    if(e.name === 'NoSuchElementError') {
        console.log('Element not found');
    }
})

And if you're using async/await, you can do something like:

try {
  await driver.findElement(webdriver.By.id("fofofo"));
  // do some task
}
catch (e) {
  if (e.name === 'NoSuchElementError')
    console.log('Element not found');
}

Upvotes: 3

Louis
Louis

Reputation: 151401

bagelmaker's suggestion of using findElements is not a bad one. That's usually what I do when I want to check whether an element exists rather than fiddle with exceptions.

However, for the sake of completeness, here is how you'd deal with the error generated when an element does not exist:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

driver.get('http://www.google.com');

driver.findElement(webdriver.By.id("fofofo")).then(null, function (err) {
    if (err.name === "NoSuchElementError")
        console.log("Element was missing!");
});

driver.quit();

The second argument to then is an errback which is called if findElement fails. Because selenium-webdriver operates with promises, this is how you have to catch the error. Using try...catch cannot work because Selenium does not start working right away; the work that you ask Selenium to do with findElement is performed at an undetermined time in the future. By that time the JavaScript execution would have moved out of your try...catch.

The code above searches for an element with an id value of fofofo, which does not exist and fails.

Upvotes: 17

bagelmakers
bagelmakers

Reputation: 409

I would recommend reading up on explicit and implicit waits. A summary, explicit waits will poll the website until a specific condition is filled, while an implicit wait is more general and will wait a specified amount of time for a command to execute. In your case, I would refer to this previous question, which was resolved by using:

driver.wait(function() {
    return driver.findElement(webdriver.By.xpath(xPath)).isDisplayed();
}, timeout);

OR

driver.wait(function() {
    return driver.isElementPresent(locator);
}, timeout);

Please comment if there is any clarification you need.

EDIT:

I mistook the question. An alternate method to catch a NoSuchElementError is to use the method findElementswhich is documented here, and has an example implementation here. If no element exists, it will return a list of size 0. This way, an exception isn't thrown, and you can determine if it exists by the size of the list.

I hope this is a better answer for your question.

Upvotes: 1

Related Questions