Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

My question is like Problem catching Selenium Webdriver NoSuchElementException but there is no answer to resolve an issue.

Class SmallLoginLocators:

public class SmallLoginLocators {
    public final String PASSWORD1 = "__nothing";
    public final String PASSWORD2 = "password";
    @FindBy (name = PASSWORD1)
    public WebElement passwordField1;
    @FindBy (name = PASSWORD2)
    public WebElement passwordField2;
}

There is password text field with different locators on two pages. Test can start from any of these pages. So I try to access that webelement:

public WebElement getPasswordField() {
    SmallLoginLocators slogin = PageFactory.initElements(wd, SmallLoginLocators.class);
    WebElement password;
    try {
        password = slogin.passwordField1;
    } catch (NoSuchElementException e) {
        password = slogin.passwordField2;
    }
    return password;
}

getPasswordField() returnes NoSuchElementException on line

password = slogin.passwordField1;

BUT!!! that is not handled by catch (NoSuchElementException e).

Q1: Why?

Q2: How to resolve?

Note: NoSuchElementException I get is from org.openqa.selenium.NoSuchElementException and imports are valid. I suspectthat the issue is in PageFactory.initElements source, but my junior knowledge of java has not given me any chance to understand.

share|improve this question
    
Why don't you post the code of PageFactory.initElements? –  l19 Nov 8 '13 at 2:44

2 Answers 2

My answer is not directly to your question. It is advice. You should not use try catch for logic realization. Program languages (Java, c#, python etc) have if ... else statements. And you can use them for your problem.

not debugged code, simply for example

if(slogin.passwordField1.Existed)
{ 
return slogin.passwordField1;
} else 
{
return slogin.passwordField2;
}

You can find how realize existed property in Internet (it is easy) and you will levelup you developer skills

share|improve this answer
    
There are times when the code your calling will throw an exception for a given circumstance that you need to be able to handle gracefully. Its not always possible to go in and re-factor or change the code to not throw an exception in that case. It could be a library or it could be a huge amount of legacy code you inherited that you have no time to refactor. This is why I do not like the concept of exceptions. They are meant to be used for things that are exceptional but often what you consider exceptional I expect may happen. –  startoftext Feb 8 at 18:55

if you have different elements locators and you want to acces to them in single way, you can use @FindAll anotations:

@FindAll(value = { 
        @FindBy(css = " ... "), 
        @FindBy(css = " ... ") 
        })
List<WebElement> elementsList;

(first element from the list will be used)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.