Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda