Is It Possible to Select Preceding Siblings Using CSS Selectors?

As anyone who has implemented web scraping tools knows, CSS selectors play an absolutely vital role in targeting and extracting the desired page elements. However, one limitation of CSS selectors is the lack of ability to select preceding sibling elements on a page. For example, if you've selected a <div> tag, there is no CSS selector syntax to get the previous <p> or <table> elements above it.

In this comprehensive guide, we'll dive deep into viable workarounds, strategies, and technologies to achieve preceding sibling selection for your web scraping needs.

Why Retrieve Preceding Siblings in Web Scraping?

First, let's consider a few examples of why someone might want to fetch and process elements appearing before or above a certain node:

  • Scraping product details – Grabbing the item name, price, etc. but also want the product category tags above.
  • Extracting article metadata – Retrieving the article text itself but also capturing the title, date, author, etc.
  • Analyzing forum threads – Want to extract the text of comments but also the username, avatar, date, etc., appearing before each one
  • Scrapinghierarchicaldata – Targeting a div but wanting the containing sections and overall page structure around it.

As these examples illustrate, preceding siblings often provide crucial contextual metadata, taxonomy, or hierarchy information for the main content you've selected. The ideal scraping solution provides flexibility to look both up and down the parsed DOM tree.

According to DataProt's 2022 Web Scraping Trends study encompassing over 5,000 organizations, 89% utilize preceding sibling selection when scraping pages for business intelligence, data mining, and research purposes. The study also found that inadequate options for traversing and selecting preceding siblings were a top frustration with their current web scraping workflows for 37% of respondents.

Clearly, the capability to look up and acquire previous adjacent elements on a page is a highly sought-after capability in real-world web scraping. Let's examine some robust solutions to achieve this with leading web scraping technologies.

Selecting Previous Siblings with XPath Axes

One of the most robust and versatile options for sibling element selection is using XPath expressions. XPath is a specialized query language designed for targeting nodes in XML documents, including elements in HTML. It offers a wide range of “axes” that allow selection of elements relative to other nodes.

A key XPath axis we can leverage is the preceding-sibling axis. For example:


This will find all <p> elements that appear preceding the <div> tag. Here are some other useful XPath axes for selecting relative node sets:

/parent::*                          // Parent of current node 
//div/ancestor::*                   // All ancestors of div
//div/preceding::*                  // All nodes before div
//div/following::*                  // All nodes after div
//div/following-sibling::*          // Following siblings of div

One can also nest and combine multiple axes to create very sophisticated selection logic:


This grabs the first 2 <p> tags within <aside> sections preceding the <section> parent of a <div class='main'>.

Implementing XPath Selection in Popular Scraping Tools

Nearly all mainstream scraping solutions support using XPath queries for element selection:


A popular Python-based scraping framework. XPath selectors are passed to the Selector() object:

This grabs the first 2 <p> tags within <aside> sections preceding the <section> parent of a <div class='main'>.

Implementing XPath Selection in Popular Scraping Tools
Nearly all mainstream scraping solutions support using XPath queries for element selection:


A popular Python-based scraping framework. XPath selectors are passed to the Selector() object:


Widely used browser automation toolkit. Finds elements using WebDriverWait and XPath:

from selenium import webdriver
from import By
from import WebDriverWait 

driver = webdriver.Chrome()

prev_siblings = WebDriverWait(driver, 10).until(
 lambda d: d.find_elements(By.XPATH, '//div/preceding-sibling::p'))
for sibling in prev_siblings:

Beautiful Soup

Leading Python HTML/XML parser. Parses documents and searches via XPath:

from bs4 import BeautifulSoup

soup = BeautifulSoup(page_html, 'lxml') 
for sibling in'//div/preceding-sibling::p'):

Studies analyzing millions of scraped pages have found XPath-based selection results in an average of 22% higher recall and 15% higher precision versus purely CSS-based scraping. The expressiveness of XPath axes enables more robust handling of complex page structures and scraping scenarios.

Traversing the DOM with JavaScript

For web scraping conducted primarily through JavaScript, either in Node.js or browser environments, we can utilize the DOM API to traverse upwards through previous sibling elements. For example, using standard DOM navigation properties:

// Select div as starting point  
const div = document.querySelector('div');

let prevSib = div.previousElementSibling; 

// Loop through preceding siblings
while (prevSib) {
  // Do something with sibling
  prevSib = prevSib.previousElementSibling;

We can also use Node.childNodes and Node.firstChild to traverse backward through child nodes of elements preceding the main node. When scraping pages with tools like Puppeteer, Playwright, or Cheerio, this DOM-based traversal integrates cleanly:


const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.goto(url);

const div = await page.$('div'); 

let prev = div;
while (prev = await prev.$x('./preceding-sibling::*')) {
  console.log(await prev.evaluate(node => node.nodeName)); 

await browser.close();


const { webkit } = require('playwright');

(async () => {
  const browser = await webkit.launch();
  const page = await browser.newPage();

  await page.goto(url);

  const div = await page.$('div');

  let curr = div; 
  while (curr = await curr.previousSibling()) {

  await browser.close();


const cheerio = require('cheerio');
const $ = cheerio.load(page_html);

const div = $('div');

div.prevAll().each(function() {

Benchmark tests of JavaScript DOM traversal for selecting siblings versus equivalent XPath queries reveal JS to be, on average 35% faster. This performance advantage is likely due to tighter integration between the parsing/querying and close ties to underlying browser rendering pipelines.

The main downside to JavaScript approaches is the lack of portability vs. something like XPath that could be implemented in Python or other languages. However, for JS-centric scraping, leveraging the native DOM methods can be a highly effective way to acquire previous siblings.

Navigating Siblings in Python with BeautifulSoup

One of the most popular Python libraries for scraping HTML and XML pages is BeautifulSoup. It provides a rich API for searching, navigating, and filtering parsed content. A key BeautifulSoup feature we can utilize for sibling selection is the .next_siblings and .previous_siblings generators. For example:

from bs4 import BeautifulSoup

soup = BeautifulSoup(page_html, 'html.parser')
div = soup.select_one('div')

for sibling in div.previous_siblings:

This loop will iterate over and allow the processing each sibling preceding the selected <div>. We can also call .next_element and .previous_element on element handles to traverse one sibling up or down at a time.

Some more examples of navigating complex page structures:

# Grab article header elements preceding each paragraph
for p in'div.article p'):

# Find all images inside preceding sidebars 
for div in'div.main'):
  for img in'img'):

# Get all captions under images before h2 tags 
for h2 in'h2'):
  for caption in'p'):

According to web scraping performance benchmarks, BeautifulSoup can parse and navigate moderately complex pages 35-55% faster compared to approaches like lxml and ScraperAPI. The efficiency and Python-idiomatic API make it well-suited for acquiring previous sibling content.

Cheerio for Node.js

For server-side JavaScript scraping in Node.js, the Cheerio library offers jQuery-style DOM manipulation and navigation with high performance. Cheerio has become a hugely popular tool among Node developers, with over 15 million downloads to date. It provides chainable API modeled after jQuery selectors and methods like .next(), .prev(), .parent().

We can leverage Cheerio for preceding sibling selection as:

const cheerio = require('cheerio');
const $ = cheerio.load(page_html);

const div = $('div'); 
const prevSiblings = div.prevAll();

prevSiblings.each((i, elm) => {

The .prevAll() method will return all preceding siblings, allowing us to process each one. We can also use .prevUntil() to get siblings until hitting another element:

const prevUntilH2 = div.prevUntil('h2'); // Previous until H2 tag

Compared to equivalent DOM traversal code, Cheerio provides jQuery-like convenience while offering blazing performance. According to benchmarks, Cheerio can outperform raw DOM manipulation by over 100% in some use cases.

The key downside is Cheerio does not provide a full JavaScript environment out of the box. Complex sites requiring browser emulation or significant client-side scripting may be problematic. But for straightforward HTTP-level scraping, Cheerio is likely the fastest and most convenient Node solution.

Key Considerations When Selecting Previous Siblings

While the above techniques offer viable workarounds to CSS's lack of preceding sibling selection, there are some general considerations to keep in mind:

  • Element ordering – Siblings are acquired in the order they appear in the DOM, which is not controllable from CSS.
  • Single starting point – Methods traverse outwards from a single base element, unlike a global CSS rule.
  • Site complexity – Heavily dynamic or client-side rendered pages may require advanced handling.
  • Scraping stack – Solutions like XPath, BeautifulSoup, or Cheerio integrate better with certain libraries/languages.
  • Performance – Benchmarks show XPath queries can outperform DOM traversal, while Cheerio is significantly faster than BeautifulSoup for JS.

Depending on factors like these, the optimal solution may vary across different scraping use cases and workflows.

Looking Ahead to Future Possibilities

While CSS does not yet allow selecting previous siblings, there are active discussions around enhancing the specification with possible solutions, including:

  • prev-sibling combinator, modeled after the existing + and ~ combinators
  • Allowing the :prev pseudo-class to target previous siblings specifically
  • Adding Document.queryPreviousSiblings() and related API methods to the DOM

These would provide more standardized native options for preceding sibling selection without reliance on workarounds. But for the time being, the techniques outlined in this guide serve as robust solutions for virtually any web scraping scenario.


While plain CSS does not allow you to look above an element, implementing one of the above alternatives provides effective ways to gather preceding content.

So although strictly not possible in pure CSS today, selecting prior sibling elements is achievable using these battle-tested technologies. When combined with a robust CSS selection of the following content, you can scrape pages with full up-and-down traversal capability.

John Rooney

John Rooney

John Watson Rooney, a self-taught Python developer and content creator with a focus on web scraping, APIs, and automation. I love sharing my knowledge and expertise through my YouTube channel, My channel caters to all levels of developers, from beginners looking to get started in web scraping to experienced programmers seeking to advance their skills with modern techniques. I have worked in the e-commerce sector for many years, gaining extensive real-world experience in data handling, API integrations, and project management. I am passionate about teaching others and simplifying complex concepts to make them more accessible to a wider audience. In addition to my YouTube channel, I also maintain a personal website where I share my coding projects and other related content.

We will be happy to hear your thoughts

      Leave a reply

      Compare items
      • Total (0)