XPath selector for several elements

XPath selector for several elements

If we need to retrieve data from a page for several similar items, for example, the cost of each product in the same category. The easiest way is to use the browser picker or the scraping feature with automatic selector creation. But besides this the user can use XPath or CSS selector.

Let’s look at an example of how to create an XPath selector.

Click the arrow in the upper left corner and then click once on the item you want to select from the page.

Right-click on the highlighted element in the HTML code and copy the XPath selector.

If we compare the XPath selectors for the cost of the first and second item in the list, we see part of the similarity and a small difference in the tag, which changes for each item in the list.

If we replace the value of this tag tr with [*], we get a selector for all similar elements on the page.

Check in the console if the selected selector is correct, the required item should be found on the page. We see that 12 elements were found by using this XPath selector.

1 Like