为什么无法从Selenium中的所有链接获取所有数据
public class Selenium {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://vnexpress.net/";
String actualTitle = "";
driver.get(baseUrl);
actualTitle = driver.getTitle();
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
WebElement li;
for(WebElement link:allLinks){
String laylink = link.getAttribute("href");
System.out.println(laylink);
driver.get(laylink);
li=driver.findElement(By.id("title_news_detail mb10"));
System.out.println(li.getText());
driver.close();
}
}
答案 0 :(得分:0)
在导航到链接URL之后,当您离开拥有所有链接的页面并关闭驱动程序时,您的代码仅适用于第一个元素。
这是应该起作用的逻辑:
public class Selenium {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,60);
String baseUrl = "https://vnexpress.net/";
String actualTitle = "";
driver.get(baseUrl);
actualTitle = driver.getTitle();
// wait for the links to display
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("a")));
// get the number of links
int numberOfLinks = driver.findElements(By.tagName("a")).size();
WebElement li;
WebElement currentLink;
// iterate through the links
String linksPage = driver.getCurrentUrl();
for (int lnkNumber=0; lnkNumber>numberOfLinks; lnkNumber++) {
currentLink = driver.findElements(By.tagName("a")).get(lnkNumber);
// get the href
String laylink = currentLink.getAttribute("href");
// print the href
System.out.println(laylink);
// navigate to laylink
driver.get(laylink);
// find li element and print the text
li=driver.findElement(By.id("title_news_detail mb10"));
System.out.println(li.getText());
// navigate to links page
driver.get(linksPage);
// wait for the links to display
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("a")));
}
}
}