通过js进行获取
// 创建一个JavaScriptExecutor对象
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String script = “var elements = document.querySelectorAll(‘div.col-lg-9 div.title.media-heading a’);” +
“return Array.prototype.map.call(elements, function(e) { return e.textContent.trim(); });”;
// 使用JavaScriptExecutor执行脚本并获取结果
Object result = jsExecutor.executeScript(script);
// 将结果转换为List<String>
List<String> postTitleList = new ArrayList<>();
if (result instanceof List<?>) {
for (Object text : (List<Object>) result) {
if (text != null) {
postTitleList.add((String) text);
}
}
}
for (int i = 9; i < postTitleList.size(); i++) {
String postTitle = postTitleList.get(i).split("\\n")[1].trim();