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 20, 2019 in Selenium by anonymous
3,299 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 12, 2020 in Selenium by MOHAMMED
• 120 points
2,580 views
0 votes
0 answers

The xpath of the element keeps changing, how do i find dynamic xpath for this

Can you please help on this, I ...READ MORE

Nov 21, 2020 in Selenium by Rakshith
• 120 points

recategorized Nov 23, 2020 by Niroj 1,689 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
7,460 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,910 points
20,853 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 30, 2018 in Selenium by sniffy_god
• 780 points
12,661 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,910 points
4,402 views
0 votes
2 answers

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,500 points
13,767 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,186 views
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,910 points
7,260 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,898 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP