DEV Community

Tony Colston
Tony Colston

Posted on

5

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay