2

i'm hoping you can help me.

I've been going through all sorts of forums and questions on here on how to cycle through multiple divs with the same class name using xpath queries. I'm fairly new to WebDriver and Java so I'm probably not asking the question properly.

I have a table where i'm trying to identify the values within and ensure they're correct. each field has the same class identifier, and i'm able to successfully pull back the first result and confirm via report logging using the following

String className1 = driver.findElement(By.xpath("(//div[@class='table_class'])")).getText();
Reporter.log("=====Class Identified as "+className1+"=====", true);

However, when i then try and cycle through (I've seen multiple answers saying to add a [2] suffix to the xpath query) i'm getting a compile error:

String className2 = driver.findElement(By.xpath("(//div[@class='table_class'])")[2]).getText();
Reporter.log("=====Class Identified as "+className2+"=====", true);

The above gives an error saying "The type of the expression must be an array type but it resolved to By"

I'm not 100% sure how to structure this in order to set up an array and then cycle through.

Whilst this is just verifying field labels for now, ultimately i need to use this approach to verify the subsequent data that is pulled through, and i'll have the same problem then

5
  • Is there a reason you want to use xpath or could you use css selectors? Commented Jan 24, 2017 at 10:46
  • No particular reason for using xpath over css selectors, but a lot of the things i've read online all use xpath so assumed this was the "most correct" way of doing it Commented Jan 24, 2017 at 10:52
  • But back to the question, could you use driver.findElements and loop through, rather than targeting each item individually? Commented Jan 24, 2017 at 11:05
  • Would you be able to give an example for me please? As i say, i'm fairly new to java and selenium so don't automatically know how to loop through the items. It sounds like what i need but i'm not sure how to write it Commented Jan 24, 2017 at 11:12
  • Look at @NarendraRajput's answer, he beat me to it Commented Jan 24, 2017 at 11:14

1 Answer 1

1

You are getting the error for -

String className2 = driver.findElement(By.xpath("(//div[@class='table_class'])")[2]).getText();

because you are using index in wrong way modify it to -

String className2 = driver.findElement(By.xpath("(//div[@class='table_class'])[2]")).getText();

or

String className2 = driver.findElement(By.xpath("(//div[@class='table_class'][2])")).getText();

And the better way to do this is -

int index=1;
List <WebElement> allElement = driver.findElements(By.xpath("(//div[@class='table_class'])"));
for(WebElement element: allElement)
{
    String className = element.getText();
    Reporter.log("=====Class Identified as "+className+""+index+""+"=====", true);
    index++
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I wasn't able to get the first two options suggested to work, but i was able to get the "better" way to work

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.