Thursday 18 April 2013

Getting XPath of Web Elements in Internet Explorer

Testing applications in Internet explorer can be tricky,for one it does not have a tool like Firebug and hence XPath finding can be difficult.But since Javascript has full power over the DOM we can write some scripts to get the element XPath by ourselves.

1)Open Internet Exploer
2)Type about:blank in the address bar and hit enter
3)From Favorites main menu select--->Add favorites
4)In the Add a favorite popup window enter name GetXPATH1.
5)Click add button in the add a favorite popup window.
6)Open the Favorites menu and right click the newly added favorite and select properties option.
7)GetXPATH1 Properties will open up. Select the web Document Tab.
8)Copy and Paste the JavaScript Snippet one code from my Git hub repository in the URL field.

9)Click Ok. Click YES on the popup alert.
10)Add another favorite by following steps 3 to 5, Name this favorite GetXPATH2 (step4)
11)Repeat steps 6 and 7 for GetXPATH2 that you just created.
12)Copy and Paste the JavaScript Snippet two code from my Git hub repository in the URL field for GetXPATH2.

13)Repeat Step 9.
 


Javascript Snippet one code:

https://github.com/Madusudanan/Selenium/blob/master/GetXPATH_init.js

Javascript Snippet two code :
https://github.com/Madusudanan/Selenium/blob/master/GetXPATH_final.js

 

You are all done!!

Now to get the XPATH of an element just select the element with your mouse. This would involve clicking the left mouse button just before the element (link, button, image, checkbox, text etc) begins and dragging it till the element ends. Once you do this first select the favorite GetXPATH1 from the favorites menu and then select the second favorite GetXPATH2. At his point you will get a confirmation, hit allow access button. Now open up a notepad file, right click and select paste option. This will give you the XPATH of the element you seek.


This has a limitation however on selecting web elements which  are like menu elements within a drop down box,hidden elements etc.But comes in very handy on a general usage scenario.

By no means this is the best way to work with locators in IE.Good stable locators are often hand crafted and carefully chosen using Xpath functions and other advanced strategies.But this is good as a starting point and to build upon further