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 in Selenium by anonymous
86 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
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,420 points
1,461 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,130 points
6,499 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
1,831 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[tex ...READ MORE

answered Jun 11, 2018 in Selenium by Samarpit
• 5,130 points
411 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,130 points
1,697 views
0 votes
1 answer

Selecting xpath for with multiple conditions using Selenium and Python

Use the XPath in either of the following ways ...READ MORE

answered Jul 12, 2018 in Selenium by Samarpit
• 5,130 points
6,021 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,110 points
965 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,130 points
1,125 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,130 points
867 views