DEV Community

Tony Colston
Tony Colston

Posted on

XPath locators with chrome devtools

XPath is one of the choices for locators with Selenium but writing XPath by hand is also harder than it should be.

You can totally let Chrome devtools do it for you!

Find the element visually on the screen
Right and choose inspect
You should not be in Chrome dev tools and the html element should be highlighted.
Right click again on the light blue highlighted html element
Choose Copy and then Copy XPath

This is input box from google front page (with a gif example!)

//*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input
Enter fullscreen mode Exit fullscreen mode

XPath is not my first choice for locators in Selenium, I will usually favor ID and Name.

XPath is tied to the structure of the DOM. If the structure changes the XPath will also change and your tests will fail.

So if you are going to use XPath know that as your web developers make changes to your website you will have to monitor your tests.

Top comments (0)