我试图获取页面上几个相似元素的 href,然后在这些 href 中找到最后创建的(最大)id。我有一个循环,但它只找到第一个元素,而不搜索其余元素。你知道里面写错了什么吗?
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);
最佳答案
您的代码片段中存在多个错误。
driver.FindElement() 将返回一个 WebElement。 - 由于您的 css 选择器对于循环的每次迭代都是相同的,因此它将始终返回相同的 WebElement。
将循环更改为如下所示:
for(WebElement el : driver.findElements(By.cssSelector("a.linkOffer"))) {
String target = el.getAttribute("href");
..
}
关于JAVA Selenium - 在循环中从 html 获取 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29556136/