4

I used Firebug's Inspect Element to capture the XPath in a webpage, and it gave me something like:

//*[@id="Search_Fields_profile_docno_input"]

I used the Bookmarklets technique in IE to capture the XPath of the same object, and I got something like:

//INPUT[@id='Search_Fields_profile_docno_input']

Notice, the first one does not have INPUT instead has an asterisk (*). Why am I getting different XPath expressions? Does it matter which one I use for my tests like:

Selenium.Click(//*[@id="Search_Fields_profile_docno_input"]);

OR

Selenium.Click(//INPUT[@id='Search_Fields_profile_docno_input']);
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
Maya
  • 7,053
  • 11
  • 42
  • 53

5 Answers5

5

*[Id=] denotes that it can be any element while the second one clearly mentions selenium to look ONLY for INPUT fields which have id as Search_Fields_profile_docno_input. The second xpath is better due to following reasons

  1. It takes more time to find the element using * as IDs of all elements should be matched.
  2. If your HTML code is not "well written" there could be other elements which have the same id and this could cause your test to fail.
A.J
  • 4,929
  • 2
  • 27
  • 36
  • 2
    Re #2: There should never be more than one element with a given ID value. In cases where there are (i.e., invalid HTML), Selenium chooses the first one in the document. – Ross Patterson Jun 18 '11 at 12:58
  • I suggest switching to CSS locators instead of XPATH since XPATH is slower in IE versions 6 and 7 – retornam Jun 18 '11 at 22:14
2

The first one matches any element with a matching ID, whereas the second one restricts matches to <input> elements. If these were CSS expressions it'd be the difference between #Search_Fields_profile_docno_input and input#Search_Fields_profile_docno_input.

Assuming you only use this ID once in your web page, the two XPaths are effectively equivalent. They'll both match the <input id="Search_Fields_profile_docno_input"> element and no other.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

There are some good answers to your "why?" question here, but for Selenium use, there's an even better alternative. Since your page element has an ID attribute, use Selenium's ID locator instead of XPath or CSS:

Selenium.Click("id=Search_Fields_profile_docno_input");

This will go directly to the element, and will run quicker than just about any other locator. Note that the syntax is id=value, not id="value".

Ross Patterson
  • 9,527
  • 33
  • 48
0

Google has just released Wicked Good XPath - A rewrite of Cybozu Lab's famous JavaScript-XPath. Link: https://code.google.com/p/wicked-good-xpath/ The rewritten version is 40% smaller and about %30 faster than the original implementation.

You can check this out and replace the one being used in Selenium.

Dominator008
  • 339
  • 4
  • 9
0

Given any element in your document, there's an infinite number of XPath expressions that will select it uniquely. Therefore it's entirely reasonable for two different products to generate two different paths.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164