4

Consider the following example, where a ul element's id is known, and we want to Click() its containing li element if the li.Text equals a certain text.

Here are two working solutions to this problem:


Method 1: Using XPath

ReadOnlyCollection<IWebElement> lis = FindElements(By.XPath("//ul[@id='id goes here']/li"));
foreach (IWebElement li in lis) {
    if (li.Text == text) {
        li.Click();
        break;
    }
}

Method 2: Using ID and TagName

IWebElement ul = FindElement(By.Id("id goes here"));
ReadOnlyCollection<IWebElement> lis = ul.FindElements(By.TagName("li"));
foreach (IWebElement li in lis) {
    if (li.Text == text) {
        li.Click();
        break;
    }
}

My question is: When should we use XPath and when shouldn't we?

I prefer to use XPath only when necessary. For this specific example, I think that XPath is completely unnecessary, but when I looked up this specific problem on StackOverflow, it seems that a majority of users default to using XPath.

budi
  • 6,351
  • 10
  • 55
  • 80
  • 1
    Use whatever you are more comfortable with! Have a read through a recent discussion here: http://stackoverflow.com/q/34521441/3124333 – SiKing Jan 14 '16 at 19:10

2 Answers2

3

In this particular case, XPath can even simplify the problem to a single line:

driver.FindElement(By.XPath(String.Format("//ul[@id='id goes here']/li[. = '{0}']", text))).click();

In general though, if you can uniquely identify an element using simple By.Id or By.TagName or other similar "simple" locators, do it. XPath expression and CSS selector based locators usually either provide advanced ways to locate elements (we can go up/down/sideways in the tree, use partial attribute matches, count elements, determine their position etc) or make the element's location concise, as in this particular situation.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0
  1. When you need to track more similar web elements use XPATH.
  2. When you need particular single element use id

Xpath having more advantage, because sometimes id get duplicate This is my experience!

Bilal Usean
  • 2,322
  • 3
  • 22
  • 45
  • 1
    But if id get duplicate, it is a bug to fix. Id needs to be unique in a html page. – Striter Alfa Jan 14 '16 at 18:55
  • id is unique OK. Just imagine, You working on thirdparty sites, here how you solve your duplicate id problem without xpath. – Bilal Usean Jan 14 '16 at 19:06
  • Even in Jquery you cannot select an duplicated ID using #. So, if you are working as tester to a thirdparty sites is your duty flag it as an error: code error and navigation error. But if you really want to ignore the duplicated id, you can use the xpath. The same as you can program using quick fix, but nobody will like to support your code, and do it will be hard some months later. – Striter Alfa Jan 14 '16 at 19:48
  • I know and Accept. Internal projects means yours best, when you test again thirdparty means xpath best.(we choose selector as per the situation) – Bilal Usean Jan 14 '16 at 20:00