XPATH's are defined by the end user. I just enabling the automation to them. Even I don't know which XPATH type will come (like name,id,xpath, css).
Is this right way to initialize web element for dynamic XPATH like below?
public WebElement initWebElement(String xpath) {
WebElement webElement = null;
try {
if (xpath.startsWith("//") || xpath.startsWith("(//")) {
webElement = webDriver.findElement(By.xpath(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.id(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.name(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.cssSelector(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.linkText(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.partialLinkText(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.className(xpath));
}
if (webElement == null) {
webElement = webDriver.findElement(By.tagName(xpath));
}
} catch (Exception e) {
e.printStackTrace();
}
return webElement;
}