6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
berdnnar
  • 101
  • 1
  • 1
  • 4
  • The question probably hints at using the ***web developer tools*** provided by [Chrome](https://en.wikipedia.org/wiki/Google_Chrome) (and other web browsers) to accomplish this. Perhaps tag it accordingly? – Peter Mortensen Nov 10 '22 at 22:37

5 Answers5

13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
heniekk
  • 374
  • 2
  • 10
  • well, Copy Xpath gives you absolute path to the element, I believe it is always better to use relative paths (if possible). So yes,as the first step click on Inspect element (right click of mouse at the element on page in Chrome) so you see the page structure around the element and in next step you can try to prepare relative path (if you hit CTRL+F in "elements" you can type xpath there and see if it returns the element) – neliCZka Jan 18 '17 at 12:07
  • @nelicCZka - Why down vote? Its first answer, and its seems to be working good for me and my code. I consider it as an solving my problem. – berdnnar Jan 18 '17 at 12:12
2

I see that someone has downvoted the suggestions to use the Copy XPath option from different inspection tools. That's probably because these paths can often be difficult to maintain as the application changes, won't work in Selenium because they're not sufficiently unique, or can just be unreasonably complex for complex HTML.

Copy XPath... is useful as a starting point though. If you're not familiar with XPath, this tool can help you to get a better feel for how to apply it to your HTML content, but it's very important to learn how to use XPath yourself.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Breaks Software
  • 1,721
  • 1
  • 10
  • 15
2

Right click on the element you want to have an XPath expression for, and then click Inspect Element. A new frame will open with the HTML source code. Now right-click on the code and select "Copy XPath".

Note: you will have to remove " WITH ' after copying it into your code

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
  • What version of Chrome? In Firefox (at least 106.0.2), it is 1) Click ***"Pick an element from the page"*** (or Shift + Ctrl + C). 2) Left click on an element on the page. 3) Right-click on the selected HTML line 4) Choose ***Copy*** → ***XPath***. – Peter Mortensen Nov 10 '22 at 21:49
  • The [context is *Inspector*](https://stackoverflow.com/questions/25363966/find-and-click-an-element-by-title-using-python-and-selenium#comment131331903_25363966). – Peter Mortensen Nov 13 '22 at 15:52
-1
  • Install Firebug for Firefox. ...
  • Open the website that you want to inspect. ...
  • Click the Firebug button. ...
  • Click the element inspector button. ...
  • Click the web page element that you want to inspect. ...
  • Right-click the highlighted code in the Firebug panel. ...
  • Select "Copy XPath" from the menu.
AJS
  • 953
  • 7
  • 21
  • I'm using Google Chrome in my project, but thanks for tips for Firefox. – berdnnar Jan 18 '17 at 12:08
  • You can do same steps with google chrome. Just press F12 button on keyboard and inspector will open then click on inspect element button on left corner . Then follow above steps. Let me know if you face any issues – AJS Jan 18 '17 at 12:10
  • 1
    "Install Firebug for Firefox" — Firebug is no long supported as described by [the big headline on the homepage for it](http://getfirebug.com/). – Quentin Jan 18 '17 at 12:25
  • we can still use it for earlier versions . And also they hv provided alternative . – AJS Jan 18 '17 at 12:28
  • This is now built into Firefox (e.g. 106.0.2): 1) Click ***"Pick an element from the page"*** (or Shift + Ctrl + C). 2) Left click on an element on the page. 3) Right-click on the selected HTML line 4) Choose ***Copy*** → ***XPath***. – Peter Mortensen Nov 10 '22 at 22:22
  • The [context is *Inspector*](https://stackoverflow.com/questions/25363966/find-and-click-an-element-by-title-using-python-and-selenium#comment131331903_25363966). – Peter Mortensen Nov 13 '22 at 15:52
-1

You can press F12 to get the check page, select your hoped HTML code, and hit the mouse 2. It has the copy option, and there is a copy XPath.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hao
  • 57
  • 1
  • 5