1

I'm having some difficulties to get multiple elements from a div. As you can see, I got the div[1] and after that I want to get more 8 div inside div[1]. But I want to do that with xpath(same line). Anyone have some idea?

Father = .//*[@id='result']/div[1]

Sons =

          .//*[@id='result']/div[1]/div[6]
          .//*[@id='result']/div[1]/div[9]
          .//*[@id='result']/div[1]/div[12]
          .//*[@id='result']/div[1]/div[15]
          .//*[@id='result']/div[1]/div[18]
          .//*[@id='result']/div[1]/div[21]
          .//*[@id='result']/div[1]/div[24]
          .//*[@id='result']/div[1]/div[27]

My Code:

List<String> productName = new ArrayList<String>();
List<WebElement> allProductsName = driver.findElements(
        By.xpath(".//*[@id='result']/div[1]/div[6]"));
for(WebElement w : allProductsName) {
    productName.add(w.getText());
}
System.out.print(productName);

I imagined something like that ".//*[@id='result']/div[1]/div[6],[9]..." or

".//*[@id='result']/div[1]/div[6 and 9 ....]"

But didn't work.

1
  • Too late but ... .//*[@id='result']/div[1]/div[position() >= 6 and position() <= 27 and position() mod 3 = 0] Commented Apr 2, 2024 at 23:54

3 Answers 3

3

as your product description comes in 6,9,12,15,18 use this code that will iterate through 6 to 27 in multiplication of 3.

List<String> productName = new ArrayList<String>();


for(int i=6; i<=27; i=i+3)
{

    List<WebElement> allProductsName = driver.findElements(By.xpath(".//*[@id='result']/div[1]/div["+i+"]"));
    for(WebElement w : allProductsName) {
        productName.add(w.getText());
    }
}
System.out.print(productName);
Sign up to request clarification or add additional context in comments.

Comments

1

Just remove the index number at the end of the xpath and get all the immediate child divs in a List.

    List<String> productName = new ArrayList<String>();
               List<WebElement> allProductsName = driver.findElements(
By.xpath(".//*[@id='result']/div[1]/div"));
               for(WebElement w : allProductsName) {
                   productName.add(w.getText());
               }

        System.out.print(productName);

4 Comments

Actually I need exactly those children because div[1] have some others div that I don't need.
Yes, like description product (Name and Price), if I get every div inside the div[1], xpath will bring the Search for example. I only need the description. div[6] = ADESIVO BASE D"AGUA FASTBOND / R$20,00 Thanks for help Grasshopper.
Can you add the relevant html to your question? html from the 5th to the 10th child div details would be enough to see if there is any unique attribute to be used in xpath.
Thanks for your help Florent, Poojan Dave solve the problem.
1

The first thing I see is that the development of the application should not have duplicate static ID. If used like this, should be a class instead.

Second thing is, the xpath expression is not good approach. Try to never use //* , and you don't need the dot (.//), as it can take much longer to find the element(s), and make tests slower.

You don't show any html code, but based on your sample you should be able to get all div element using

List<WebElement> results = driver.getElements(By.id("result"));

or if you insist on xpath

List<WebElement> results = driver.getElements(By.xpath("//div[@id='result']"));

3 Comments

Thanks for your help Florent, Poojan Dave solve the problem.
Your welcome. In any case though, just more input if its already solved.
I guess the down vote because people don't know how to parse the list after getting all the list items?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.