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; }
Hi Karthik,
Anyhow the end user will define only xpath, so there is no point in searching By (classname, id, linktext)
Rather you can check the xpath is valid by using below logic
Functions below will returns webelement, by verifying user define xpath is valid.
public Webelement getobject( String userxpath)
{
List<Webelement> e= driver.findelements(By.xpath(“userxpath”)
if(e.size()>0)
{
return e;
}
else
{
System.out.println(“Object not present in web page”);
}
}
Regards,
Chetan
Sent from Mail for Windows 10
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/15bf7503-6aca-48aa-8094-f978f61e4efb%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.
public WebElement initWebElement(By locator) {
WebElement webElement = null;
try {
webElement = webDriver.findElement(locator);