I need to make a test to login on 5 different sites. The issue is that some of them have a login button, and the others display directly the login form, to fill the username and password.

The only thing in common between those sites, that has a login button is that their class contains a login text in the name. I'm using the POM model for the pages and identify the elements in the following way:

@FindBy(how = How.ID, using = "txtPassword")
private WebElement passwordField;

Is there a way to find those login buttons using contains? What would be the best practice to handle this kind of operation?

3 Answers 3


If you need to go by something else than id, check this link.

In your case, you can use className or xpath or css - depending on your exact HTML code. So for example, you could have something like:

@FindBy(how = How.className, using = "login")
private WebElement passwordField;

This way, you can define the login buttons in a broader sense so all of them match a single definition. Of course, like Peter's reply suggests, if these are different pages and you want to apply the POM strictly, you could have a different class for each page. Might seem more work to maintain at first, but what if one of your five pages changes the HTML class?

  • You don't need always use 'how=....' You can simply use @FindBy(css="login") , this is exactly the same as @FDM wrote, but short version.
    – wmarchewka
    Commented Jul 28, 2015 at 7:20

best practice will be to use page object design pattern. Then, each page has login method, which does login in a page-specific way.


You always can use xpath, for example simple xpath for button with text "Login" will be (with findby of course):

private WebElement loginButton;

Or if You want to locate button that contains few words and You want to use only for example two of it, this will be:

@FindBy(xpath=".//button[contains(text(), 'Login to')]")
private WebElement FirstLoginButton;

Your Answer

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

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