硒:线程中的异常;“主要”;org.openqa.selenium.StaleElementReferenceException:

硒:线程中的异常;“主要”;org.openqa.selenium.StaleElementReferenceException:,selenium,Selenium,我正在努力点击导航链接(标记为1,2,…下一步),以便在dice.com网站上进行特定搜索 当我运行下面提到的代码时,它执行一次,然后显示StaleElementReferenceException 请您帮助解决此问题 import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import o

我正在努力点击导航链接(标记为1,2,…下一步),以便在dice.com网站上进行特定搜索

当我运行下面提到的代码时,它执行一次,然后显示StaleElementReferenceException

请您帮助解决此问题

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Ex2 {  
public static void main(String[] args) {

    WebDriver driver=new FirefoxDriver();
    driver.get("http://dice.com");
       driver.findElement(By.xpath("//input[@id='FREE_TEXT']")).sendKeys("Selenium");
    driver. findElement(By.xpath("//*[@id='searchSubmit']")).click();

    //block that has navigation links
    WebElement b=driver.findElement(By.xpath("//*[@id='yui-main']/div/div[1]/div[1]/div[1][@class='pageProg']"));

              //navigation links
    List<WebElement> allLinks=b.findElements(By.tagName("a"));
    System.out.println("Total links -->" + allLinks.size());    


    for(int i=0;i<allLinks.size();i++){

        allLinks.get(i).click();
        Thread.sleep(5000);
    }

}

}
import java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类Ex2{
公共静态void main(字符串[]args){
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://dice.com");
findElement(By.xpath(//input[@id='FREE_TEXT'])).sendKeys(“Selenium”);
driver.findelelement(By.xpath(“/*[@id='searchSubmit']”)。单击();
//具有导航链接的块
WebElement b=driver.findElement(By.xpath(“/*[@id='yui-main']/div/div[1]/div[1]/div[1][@class='pageProg']”);
//导航链接
列出所有链接=b.findElements(按.tagName(“a”));
System.out.println(“总链接-->”+allLinks.size());
对于(int i=0;i
构建信息:版本:“2.35.0”,修订版:“8df0c6b”,时间:“2013-08-12 15:43:19”
系统信息:os.name:'Windows 8',os.arch:'amd64',os.version:'6.2',java.version:'1.7.0_11'

会话ID:0410f597-c149-46b5-a2b7-e84c61cc73f1将此用作解决方法

try{
//Your code which causes exception

}
catch(org.openqa.selenium.StaleElementReferenceException e){
//Repeat the code in try
}

出现异常的原因是javascript再次加载了具有相同名称或id或其他内容的元素,而您仍在引用当前不存在的元素。

问题在于,当您单击第一个链接时,页面被重新加载,Selenium对页面的引用变得过时。我认为这种方法对y有效其他指定用途:

List<WebElement> allLinks=b.findElements(By.tagName("a"));
System.out.println("Total links -->" + allLinks.size());    

String[] linkText = new String[allLinks.size()];

for(int i=0;i<allLinks.size();i++)
{
    linkText[i] = allLinks.get(i).text;
}

for(int i=0;i<linkText.length;i++)
{
    findElements(By.linktext(linkText).click();
    Thread.sleep(5000);
}
List allLinks=b.findElements(按.tagName(“a”));
System.out.println(“总链接-->”+allLinks.size());
String[]linkText=新字符串[allLinks.size()];
对于(int i=0;i