Initialize WebElement for dynamic XPath

100 views
Skip to first unread message

Karthik

unread,
Aug 19, 2019, 10:28:45 AM8/19/19
to Selenium Users

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;
}

Chetan Baraskar

unread,
Aug 19, 2019, 11:07:19 AM8/19/19
to seleniu...@googlegroups.com

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.

 

Karthik

unread,
Aug 19, 2019, 12:25:37 PM8/19/19
to Selenium Users
Thank you for the response. They will not give only xpath. It contains css/xpath/id/classname or either other. That means unknown selector type. So that, I created that method. Is it good way to do that? 

To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

Joseph Conlin

unread,
Aug 20, 2019, 2:12:15 PM8/20/19
to Selenium Users
Instead of having them pass in a String object and have you try to figure out which kind of By locator they are trying to use, you could have them pass in a By variable that already includes what kind of locator it is.

They would do something like this:
By myVariableName = By.xpath("//*[@id='test']);
By anotherVariableName = By.id("test1");

WebElement myFirstElement = initWebElement(myVariableName);
WebElement anotherWebElement = initWebElement(anotherVariableName);

Your code would look something like this:
public WebElement initWebElement(By locator) {
 
WebElement webElement = null;
 
try { 
webElement = webDriver.findElement(locator);
Reply all
Reply to author
Forward
0 new messages