selenium

How can I get the XPath expression from a web site's HTML content?


I want to use XPath expressions in Selenium code, but I don't know how to get it from a site's HTML code.

I'm using the Google Chrome web browser.


Solution

  • The easiest way is to inspect the element you want to get the XPath expression for. Then click on the highlighted code and CopyCopy XPath which give you a full XPath patch you can copy to your code. For Firefox, you should install Firebug or another extension like this.

    As neliCZka suggests, you could also try to build a relative path if its possible by trying to find the proper XPath in the search bar in Chrome after inspecting the element.