How do I catch Selenium Errors using WebDriverJS
javascript, selenium, selenium-webdriver, try-catch
Solution
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.
Problem
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'); ```