Home > Back-end >  I need a solution to xpath
I need a solution to xpath

Time:11-06

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[@]')[0].find_element_by_xpath('.//following::div')

CodePudding user response:

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)

CodePudding user response:

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related