Java 为什么可以';无法从Selenium的链接获取所有数据
为什么不能从Selenium中的所有链接获取所有数据Java 为什么可以';无法从Selenium的链接获取所有数据,java,selenium,Java,Selenium,为什么不能从Selenium中的所有链接获取所有数据 public class Selenium { /** * @param args the command line arguments */ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); WebD
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();
}
}
公共类Selenium{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
System.setProperty(“webdriver.chrome.driver”,“C:\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
字符串baseUrl=”https://vnexpress.net/";
字符串ActualTile=“”;
get(baseUrl);
actualTitle=driver.getTitle();
列出所有链接=driver.findElements(按.tagName(“a”));
网络元素李;
for(WebElement链接:所有链接){
字符串laylink=link.getAttribute(“href”);
系统输出打印项次(laylink);
driver.get(laylink);
li=driver.findElement(按.id(“标题新闻细节mb10”);
System.out.println(li.getText());
driver.close();
}
}
在导航到链接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")));
}
}
}
运行程序时出现错误。我无法获取所有数据。哪一行引发了错误,错误消息是什么?消息错误:线程“main”org.openqa.selenium.TimeoutException中出现异常:预期条件失败:等待位于的元素存在:by.xpath:a(以500毫秒的间隔尝试60秒)在org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)在org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)在selenium.selenium.main(selenium.java:33)由以下原因引起:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“a”}