protractor icon indicating copy to clipboard operation
protractor copied to clipboard

findElement error handling?

Open mavdi opened this issue 12 years ago • 3 comments

How would I go about clicking a button if it exists and ignoring it if it doesn't?

this.ptor.findElement(this.by.linkText('/logout'))
     .then(function(element) {
      element.click().then(callback);
     })

isElementPresent won't do it for me, because I want to handle a case where the element is not present. Almost like

this.ptor.isElementNotPresent(someElement).then(callback);

Any sort of error handling on the promise? Any other way of doing this?

mavdi avatar Feb 03 '14 12:02 mavdi

Error handler is within the then function:

this.ptor.findElement(this.by.linkText('/logout'))
     .then(function(element) {
      element.click().then(callback);
     }
     , function() {
          callback()
     });

I wish everyone just agreed on how to do promises.

mavdi avatar Feb 03 '14 12:02 mavdi

I tried below code and it still it failing if element not found on the page, ptor.findElement(protractor.By.xpath('//*[@id="dLabel"]')).then(function(element) { element.click().then(callback); } , function() { callback() });

smahalingam avatar Feb 11 '14 12:02 smahalingam

Use Try Catch block. Try the below code: browser.driver.findElement(by.id(browser.params.loginPage.IdTextBoxCode)).then(function(codeTextBox) { try { console.log("Entering Code: "+code); codeTextBox.sendKeys(code); } catch(err) { console.log('In catch block'); } }, function(err) { console.info('Code Text Box not displayed on page. Proceeding with default Code'); });

sakshisingla avatar Sep 30 '14 04:09 sakshisingla