XPATH: locating elements using xpath

Last updated on:

There is few different ways to locate element on the page using XPATH.
First is most simple one:

public class HomePageContainer {

WebDriver driver = new FirefoxDriver();

public static void navigateToLoginPage(){
    driver.findElemnt(By.xpath("//*[@id="login"]")).click();
   }
}

But when it comes to developing test automation framework I would rather use next two, just because it is giving you flexibility and proper lair of abstraction:

public class HomePageContainer {
    @FindBy(how = How.XPATH, using = "//*[@id="login"]")
    public WebElement loginURL;

public static void navigateToLoginPage(){
    loginURl.click();
   }
}
public class HomePageContainer {
    @FindBy(xpath = "//*[@id="login"]")
    public WebElement loginURL;

public static void navigateToLoginPage(){
    loginURl.click();
   }
}
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.