NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I select sibling elements in XPath?

Find an anchor element, then step sideways with the following-sibling:: or preceding-sibling:: axis, usually limited to the nearest one with [1].

The direct answer

Siblings are elements that share the same parent. XPath reaches them with two axes: following-sibling:: for those after the current node and preceding-sibling:: for those before it. A typical expression is //dt[.="Color"]/following-sibling::dd[1], which finds the term "Color" in a definition list and returns the first dd right after it.

Why siblings matter in scraping

Specification blocks rarely give each value its own class. You see markup like this:

<dl class="specs">
  <dt>Material</dt><dd>Oak</dd>
  <dt>Depth</dt><dd>42 cm</dd>
  <dt>Assembly</dt><dd>Required</dd>
</dl>

The label is the only stable anchor. Position (the second dd) breaks as soon as the shop adds or removes a row, but the label "Depth" stays. Sibling axes let you anchor on the label and read the value beside it.

The [1] trap

following-sibling::dd without an index returns every dd after the anchor, not just the next one. In the list above, starting from "Material" it would return Oak, 42 cm and Required. Always add [1] when you want the neighbor. With the reverse axis, preceding-sibling::dt[1] is the closest one before the node, because reverse axes count backwards from the current position.

Patterns worth keeping

GoalExpression
Value cell next to a header cell//tr/th[normalize-space()="SKU"]/following-sibling::td[1]
Label before an input//input[@name="qty"]/preceding-sibling::label[1]
All items after a heading, up to the next heading//h3[.="Returns"]/following-sibling::p[preceding-sibling::h3[1][.="Returns"]]
Any next element whatever its tag//span[@class="was"]/following-sibling::*[1]

The third row is the classic "section between two headings" trick: keep only paragraphs whose nearest preceding h3 is the one you started from.

Running it in Python

from lxml.html import fromstring as parse_markup

page = parse_markup(product_markup)
def spec(label):
    hit = page.xpath(f'//dl[@class="specs"]/dt[normalize-space()="{label}"]/following-sibling::dd[1]')
    return hit[0].text_content().strip() if hit else None

print(spec("Depth"), spec("Weight"))

The helper returns None for labels that a product lacks, so missing fields do not crash a batch run. Beautiful Soup has no XPath, but its find_next_sibling() method does the same job; see XPath and Beautiful Soup for mixing the two.

Siblings are not cousins

A common surprise: in a table, a td in the next row is not a sibling of a td in this row, because the two cells have different parents. To reach the row below, go up first: ancestor::tr[1]/following-sibling::tr[1]/td[2]. When siblings fail, sketch the tree and check which elements really share a parent. Also watch for whitespace text nodes: following-sibling::node() includes them, while following-sibling::* skips straight to elements, which is nearly always what you want.

Siblings in Selenium and Playwright

Browser tools accept the same axes, so the patterns above carry over unchanged. In Selenium pass the expression with By.XPATH; in Playwright prefix it with xpath=. Relative paths work from an element you already hold: calling find_element(By.XPATH, "./following-sibling::td[1]") on a header cell returns the cell beside it. Keep the leading dot, because an expression that starts with // searches the whole document again even when called on an element, a classic source of wrong values in loops.

Testing expressions quickly

In Chrome or Firefox DevTools, run $x('your expression') in the console to see the matches live. Test on the HTML your scraper really receives, though: if the content is built by JavaScript or hidden behind a challenge page, the browser view can mislead you. The DOM entry explains how rendered and raw documents differ.

Turning a spec block into a dictionary

When you need every pair rather than one value, do not run a separate sibling query per label. Select all the labels once, and for each label take its first following value sibling. In lxml that is a loop over //dl[@class="specs"]/dt, calling dt.xpath("following-sibling::dd[1]") on each node. The result maps every label to its value in one pass, including labels you did not know about in advance, which is useful when products in the same shop list different attributes.

Store the raw label text as the key and normalize it later (trim, lower-case, map synonyms like "Colour" and "Color"). Keeping the original wording in your data makes it easy to spot when the site renames a field, instead of silently losing that column.

Common questions

What is the difference between following and following-sibling?

following:: returns every node after the current one anywhere in the document, while following-sibling:: stays under the same parent.

Why does preceding-sibling::*[1] give the nearest element?

Reverse axes number their nodes starting from the one closest to the context node.

Can CSS select siblings too?

CSS has + and ~ for later siblings, but it cannot select earlier siblings or start from text, which XPath can.

How do I get the value two cells to the right?

Use following-sibling::td[2], which counts element siblings after the anchor.

Do sibling axes work in Selenium?

Yes. Selenium passes XPath to the browser engine, which supports all XPath 1.0 axes.

Keep reading