gpt4 book ai didi

java - 为什么我的所有页面数据都显示 StaleElementReferenceException

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:40 25 4
gpt4 key购买 nike

显示堆栈溢出错误

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com