Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
I need a solution to xpath

I there a condition that i can set to break loop when i reach next category?, i was wondering what is equivalent method for xpath to jquery nextUntil

<div class="category">Category one</div>
<div>category item 1</div>
<div>category item 2</div>
<div>category item 3</div>
<div class="category">Category two</div>
<div>category item 1</div>
<div>category item 2</div>
<div>category item 3</div>
<div>category item 4</div>

i need xpath that will select items after category one until category two, and the from category two to category n

driver.find_elements_by_xpath('.//div[@class="category"]')[0].find_element_by_xpath('.//following::div')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is manual implementation of your request, read inline comments. Obviously, this is one of multiple ways of doing what you need.

// Custom function to select FROM TO category
const selectFromTo = (from, to) => {
  // Select all divs
  const divs = document.getElementsByTagName("div");
  // Loop divs
  const result = [];
  let counter = 0;
  for(const div of divs) {
    // If there is classname "category" do count
    if(div.className === "category") counter++;
    // Check from/to number and push to results
    else if(counter >= from && counter < to) result.push(div);
  }
  // Return result
  return result;
}

// Select range
// First digit is start category,
// second is TILL this category number
const test = selectFromTo(2, 4);

// View selected
console.log(test);
<div class="category">Category one</div>
<div>category item1 1</div>
<div>category item1 2</div>
<div>category item1 3</div>
<div class="category">Category two</div>
<div>category item2 1</div>
<div>category item2 2</div>
<div>category item2 3</div>
<div>category item2 4</div>
<div class="category">Category three</div>
<div>category item3 1</div>
<div>category item3 2</div>
<div>category item3 3</div>
<div>category item3 4</div>
<div class="category">Category four</div>
<div>category item4 1</div>
<div>category item4 2</div>
<div>category item4 3</div>
<div>category item4 4</div>
<div class="category">Category five</div>
<div>category item5 1</div>
<div>category item5 2</div>
<div>category item5 3</div>
<div>category item5 4</div>

about 4 years ago · Juan Pablo Isaza Report

0

In your case, I would group all the category Items into a new parent div, and then just go through them.

Example:

<div class="category">Category one</div>
<div id="category-1">
  <div>category item 1</div>
  <div>category item 2</div>
  <div>category item 3</div>
</div>
<div class="category">Category two</div>
<div id="category-2">
  <div>category item 1</div>
  <div>category item 2</div>
  <div>category item 3</div>
  <div>category item 4</div>
</div>

EDIT: If you aren't able to change the HTML structure (I've just seen the Selenium tag so I suppose that you might not be able to), the easier method would be just iterating through the HTML elements and do it without XPath. XPath isn't made for what you're trying to achieve.

Pseudocode:

categories = []
current_category = []
for element in elements:
    if element.class is "category":
        if len(current_category) != 0:
             categories.append(current_category)
             current_category = []
    else:
        current_category.append(element)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!