Search for elements by XPath. Mini guide.

Updated Apr 3, 2024

An xpath is a string that exactly identifies an element on a page. It is used to search for certain HTML blocks.

Each element can be found by XPath.
Each element can be found by XPath.

XPath Search Tools

Every modern browser, be it IE, Firefox, Chrome, Opera or Safari, allows you to search for items by XPath. The code below will scroll to the element with the XPath “HTML/BODY/DIV[2]/A[5]” and highlight it in red. Execute it in the browser console. If the element is not found, the code will try to find its parent.

var x = "HTML/BODY/DIV[2]/A[5]";
for(var i = 0;x.length > 4;++i) {
    var e = $x(x);
    x = x.replace(/\/[^\/]+$/g, "");
    if(e.length === 0) continue;
    e[0].scrollIntoView({block: "center"});
    e[0].style.boxShadow = "0 0 10px 10px red";
    break;
}
if(i) alert("The match is not exact. The "+i+" parent of the searched element was found.");

Pressing the F12 button opens the console. On the Internet you can find detailed instructions for your browser on how to use it.

We recommend using special plug-ins. These specialized extensions provide a simple interface for searching and highlighting found items. It is very convenient and speeds up the work of a specialist.

Plugin interface for XPath search.
Plugin interface for XPath search.

Recommended plugins: - Chrome https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl - Firefox [https://addons.mozilla.org/en-US/firefox/addon/try-xpath/](https://addons.mozilla.org/en-US/firefox/addon/try-xpath / “Firefox plugin for XPath search”)

Sometimes XPath may lose relevance for the following reasons: - You are looking for dynamically generated content. In this case, the XPath string may lose relevance. - The page for the logged-in user and the guest may differ. - The content of the site may be updated.

If you couldn’t find the item

Each element is inscribed inside another element. In fact, the pages consist of rectangles of different sizes inscribed into each other.
Each element is inscribed inside another element. In fact, the pages consist of rectangles of different sizes inscribed into each other.

The XPath string is divided into sections and has a cascading structure. If the full line points to a specific element, then part of it points to one of the parent blocks. If you can’t find the item, then try removing a few sections from the end of the line. This is how you will find one of the parent blocks. This will help you understand the reasons why the search is not working.

The block with the rating on the first line of the result may or may not be displayed. In this case, it makes sense to find the parent block by removing several sections from the end of the line.
The block with the rating on the first line of the result may or may not be displayed. In this case, it makes sense to find the parent block by removing several sections from the end of the line.

As you can see XPath is a pretty simple thing. There are tools for working with it on literally every personal computer. That’s why we use it in our tools.

We use cookies. By continuing to use the site, you agree to the processing of personal data in accordance with privacy policy. I agree