Reputation: 149
I'm trying to get hrefs of few similiar elements on the page and then find the last created (max) id in these hrefs. I have got a loop but it only finds the first element and does not search for the rest of them. Do you know what's written wrong in it?
ArrayList<Integer> tablicas = new ArrayList<Integer>();
int i;
for(i = 0; i < 90; i++) {
String s = driver.findElement(By.cssSelector("a.linkOffer")).getAttribute("href");
String p = s.substring(19, s.length());
int numer = Integer.parseInt(p);
System.out.print(p);
for(int indeks : tablicas) {
if(indeks == numer) {
continue;
} else {
tablicas.add(numer);
}
}
} System.out.print(tablicas);
Upvotes: 0
Views: 181
Reputation: 177
If you want to find the all the elements which contains text 'Nowa oferta dokument', use driver.findElements
(which will return list of elements matching your search criteria) rather than findElement
and loop through the all elements.
ArrayList<Integer> tablicas = new ArrayList<Integer>();
java.util.List<WebElement> elements = driver.findElements(By.xpath("//*[text()='Nowa oferta dokument']"));
for(WebElement element : elements)
{
element.getAttribute("href");
String p = s.substring(19, s.length());
int numer = Integer.parseInt(p);
System.out.print(p);
for(int indeks : tablicas) {
if(indeks == numer) {
continue;
}
else {
tablicas.add(numer);
}
}
}
System.out.print(tablicas);
Hope this helps.
Upvotes: 0
Reputation: 762
There are multiple errors in your code snippet.
driver.FindElement() will return one WebElement. - Since your css selector is identincal for each iteration of the loop, it will always return the same WebElement.
Change your loop to something like this:
for(WebElement el : driver.findElements(By.cssSelector("a.linkOffer"))) {
String target = el.getAttribute("href");
..
}
Upvotes: 1