int i, j;
int max1 = 11, max2 = 8;
for(i = 1; i <= max1; i++) {
for(j = 1; j <= max2; j++) {
String myXPath = "/html/body/div[2]/div[2]/div[2]/div[2]/
div["
+ j + "]/div/div[3]/div/div[" + i + "]/div[2]/div[1]/
div";
try {
driver.findElement(By.xpath(myXPath)).click();
// put the rest of your code here
} catch(Exception e) {
}
}
}
In this example, I have set it so that i will range from 1 to 11
(max1) and j will range from 1 to 8 (max2). If you want to change the
maximum value for i, change the max1 = 11. If you want to change the
maximum value for j, change the max2 = 8.
There are issues with this code. For example, I never catch Exception.
If I am expecting NoSuchElementException then I'll catch
NoSuchElementException. If you catch Exception you might be catching
an exception you are not aware of and not handling correctly. This
leads to things every tester has seen. You get a log message that says
"code failed because of XXX." but when you investigate you find out it
failed for reasons completely different from XXX and there is no stack
trace to help you debug it.
Also, since we don't have the HTML you are trying to automate, I
wonder if using a nest for loop is the best solution or can you create
a single XPath, findElements then an iterator to go through the list,
e.g.
String mySingleXPath = "???";
List<WebElement> elements =
driver.findElements(By.xpath(mySingleXPath));
Iterator<WebElement> i = elements.iterator();
while(i.hasNext()) {
WebElement element = i.next();
element.click();
}
Lot cleaner and easier for a Java programmer to read.
Darrell