我需要一页一页地解析页面,而下一个按钮是可点击的。我默认连接到第一页并解析该页面的最后一个元素:
driver.get("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
我从首页获取所有元素:
WebElement tableId = driver.findElement(By.tagName("table"));
List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));
最后尝试检查下一个按钮是否可单击,然后从当前页面和其他剩余页面(如果存在)进行解析:
WebElement nextButton = driver.findElement(By.xpath("//img[contains(@title,'next')]"));
if(isClickable(nextButton, driver)) {
for (int id = 1; id <= trElements.size(); id++) {
for (WebElement element : trElements) {
//...
id++;
}
}
} else {
nextButton.click();
}
当我遍历当前第一页的循环时 - 一切都很好,但如果有必要,我需要检查下一个按钮以再次解析另一个页面。
isClickable()
方法如下所示:
public static boolean isClickable(WebElement el, WebDriver driver) {
try {
WebDriverWait wait = new WebDriverWait(driver, 1);
wait.until(ExpectedConditions.elementToBeClickable(el));
return true;
} catch (Exception e) {
return false;
}
}
感谢 suggestion by Alexey R 解决像:
After you do element.click(); your DOM gets rebuilt so after that you have your trElements stayed filled with stale elements.
driver.get("https://www.scimagojr.com/journalrank.php?country=UA&page=1");
List<Journal> journalList = new ArrayList<>();
//...
for (int i = 0; i < countOfPages; i++) {
WebElement tableId = driver.findElement(By.tagName("table"));
List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));
for (int id = 1; id <= trElements.size(); id++) {
WebElement element = trElements.get(id);
String title = element.findElement(By.xpath("./td[2]/a")).getText();
String country = "Ukraine";
journalList.add(new Journal(id, title, country));
}
WebElement element = driver.findElementByXPath("(//div[@class='pagination_buttons']/a)[2]");
element.click();
}
因此,我将每个页面都可以更改的动态元素放入循环中以使其正常工作。
我是一名优秀的程序员,十分优秀!