How to handle element not found exception in Protractor

前端 未结 3 1036
长情又很酷
长情又很酷 2020-12-29 12:04

Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.

If we want to handle element not fo

3条回答
  •  北海茫月
    2020-12-29 12:34

    Try, Catch has the following syntax in Protractor. The below code will first find an element by Id 'IdTextBoxCode'. Then the code to enter code 'codeTextBox.sendKeys(code);' is in TRY block. If the code throws exception(in this case, if the element with Id 'IdTextBoxCode' is not found), then it will go to the catch block and the error handling function.

    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');
        });
    

提交回复
热议问题