显示堆栈溢出错误
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver","/home/arima/chromedriver/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get("http://education-india.in/Education/Courses/?PageNumber=1");
Thread.sleep(5000);
List<WebElement> dropdown = driver.findElements(By.xpath("//select[@id='txtPageNumber']/option"));
for(int k=1;k<dropdown.size()-1;k++) {
List<WebElement> rows = driver.findElements(By.xpath("//table[@class='index']/tbody/tr"));
List<WebElement> col = driver.findElements(By.xpath("//table[@class='index']/tbody/tr[1]/th"));
for(int i=0;i<rows.size()-1;i++){
System.out.println(rows.get(i).getText());
}
dropdown.get(k).click();
Thread.sleep(4000);
/*
* WebDriverWait wait = new WebDriverWait(driver, 10);
* wait.until(ExpectedConditions.presenceOfElementLocated(dropdown));
*/
}
}
最佳答案
stale element reference: element is not attached to the page document
当 webdriver 无法识别该页面上的元素时,就会出现错误。您需要在 for 循环内重新分配下拉元素。请尝试以下代码。
driver.get("http://education-india.in/Education/Courses/?PageNumber=1");
WebDriverWait wait=new WebDriverWait(driver, 30);
List<WebElement> dropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));
for(int k=1;k<dropdown.size()-1;k++) {
List<WebElement> newdropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));
List<WebElement> rows = driver.findElements(By.xpath("//table[@class='index']/tbody/tr"));
List<WebElement> col = driver.findElements(By.xpath("//table[@class='index']/tbody/tr[1]/th"));
for(int i=0;i<rows.size()-1;i++){
System.out.println(rows.get(i).getText());
}
newdropdown.get(k).click();
}
关于java - 为什么我的所有页面数据都显示 StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56020600/