Resolve web Locator in Selenium Java

Can we write a class in our Selenium framework to identify witch locator is passed by the user? It means.

public class ResolveLocator {
    static WebDriver driver;
    static String uiUsernameTXT =   "//input[@id='username']";

    public static WebElement ResolveLocatorL(String uiElement) {

        String checkLType = uiElement.toString();
        WebElement returnLType = null;

        switch (checkLType.charAt(0)) {
        case '/':  returnLType = driver.findElement(By.xpath(uiElement)); break;
        case 'c':  returnLType = driver.findElement(By.cssSelector(uiElement)); break;
        case 'i':  returnLType =driver.findElement(;; break;
        case 'l':  returnLType = driver.findElement(By.linkText(uiElement)); break;

        return returnLType; 

If user passes a XPath as a locator this class have to identify and return a correct code block of selenium.

1 answer

  • answered 2017-10-11 10:00 murali selenium

    rather than charAt(0), we can use naming convention to find it easily

    uiUsernameTXT_xpath= //so and so

    as we are writing as strings. split the string with "_" and go for last one and return location type. else good to use By operator

    static By uiUsernameTX=By.xpath(".//* so and so");