1

I am submitting a form with selenium and phantomjs and then navigating back to the previous page by searching for a br element indicating that the form has been submitted. Occasionally the timeout will exceed the 30s allotted time and an error will occur: TimeoutError: Waiting for element to be located By(xpath,//div[@id='ContactFormBody']/div/br) Wait timed out after 30003ms

I need to handle this error so that my program will not crash if this occurs. Additionally if anyone wants to mention good documentation for selenium node.js, that would be great!

// code generating wait error
form.then(function(self){
    driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000)
    .then(function(){
        driver.navigate().back();
    });
});

// attempt at handling error
form.then(function(self){
    try{
        driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000)
        .then(function(){
            driver.navigate().back();
        });
    }
    catch(e){
        console.log("error occurred, script will continue.")
    }
});
5
  • Waiting for br that would indicate the form submission..does not sound very reliable..is there any other form submission indication on the page? Thanks. Commented Dec 7, 2016 at 18:30
  • I agree! I was initially trying to wait/watch form the form fields to disappear which is another indication that the form was submitted. Could not find any examples or documentation for this condition. Commented Dec 7, 2016 at 18:32
  • The br is a constant though its added in place of the form fields followed by text after form submission. Commented Dec 7, 2016 at 18:33
  • Alright, have you tried other browsers - is it consistently failing in chrome or firefox as well? Thanks. Commented Dec 7, 2016 at 18:44
  • Yes, actually the script take a cli argument that specifies the browser ( phantom, chrome, firefox ) Commented Dec 7, 2016 at 18:47

1 Answer 1

2

You can specify an error callback function as a second argument to then():

driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000)
    .then(function() {
        driver.navigate().back();
    }, function (error) {
        console.log("Error happened!");
        console.log(error);
    });
});

This documentation page has a good overview of using promises in WebDriverJS:

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, thanks for your help this is exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.