Initialize WebElement for dynamic XPath

0 votes

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;
}
Aug 19, 2019 in Selenium by anonymous
652 views
Have you tried running this script and is it throwing any error?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Selenium

0 votes
0 answers

XPATH FOR DYNAMIC DROP DOWN

Hi all, I am facing some challenges in ...READ MORE

Jul 11 in Selenium by MOHAMMED
• 120 points
87 views
0 votes
1 answer

XPath for the elements with no ID or Name in Selenium Webdriver

Use like this or similar to this: //div[@id="top-level-menu-item-3"]/div[@class="filter-label"] //div[@id="top-level-menu1"] ...READ MORE

answered Apr 17, 2018 in Selenium by Meci Matt
• 9,460 points
2,623 views
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,890 points
11,193 views
0 votes
1 answer

Between By.XPath and By.ID, which is the better for locating elements?

Technically speaking, By.ID() is the faster technique ...READ MORE

answered May 29, 2018 in Selenium by sniffy_god
• 780 points
3,701 views
0 votes
1 answer

How to resolve the issue: Xpath for node without text but child has text

This will help you: //strong[not(normalize-space(text()))]/span[te ...READ MORE

answered Jun 11, 2018 in Selenium by Samarpit
• 5,890 points
1,057 views
0 votes
1 answer

Getting the proper xpath for date picker in selenium

Try to use explicit wait. Sometimes it ...READ MORE

answered Jul 5, 2018 in Selenium by Samarpit
• 5,890 points
2,518 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
3,133 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,364 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
2,512 views