Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath different in IE and Firefox. Why?

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']);
like image 643
Maya Avatar asked Feb 23 '23 20:02

Maya


2 Answers

*[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.
like image 169
A.J Avatar answered Mar 02 '23 16:03

A.J


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.

like image 25
John Kugelman Avatar answered Mar 02 '23 17:03

John Kugelman