当前位置: 首页 > 知识库问答 >
问题:

org.openqa.selenium.staleElementReferenceException:过时元素引用:元素未附加到页面文档

金飞
2023-03-14
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=39.0.2171.99)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)    (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 602 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html 
 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
 System info: host: 'Casper-PC', ip: '10.0.0.5', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_25' 
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Users\Casper\AppData\Local\Temp\scoped_dir7476_21861},   takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true,  version=39.0.2171.99, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 1e0f05fdc64ecd5720f0d050a602ee4b
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:152)
at com.Trial.SortingTable.main(SortingTable.java:36)
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class SortingTable {
    public static void main(String[] agrs) throws InterruptedException{
    WebDriver driver = null;
    try{
    System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
    driver= new ChromeDriver();
    driver.manage().window().maximize();
    //Authentication pop-up
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/");
    driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("Gmail Username");
    driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("Gmail Password");
    driver.findElement(By.xpath(".//*[@id='signIn']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    WebElement wb = driver.findElement(By.xpath(".//*[@id=':j']"));
    wb.click();
    driver.findElement(By.xpath(".//*[@id=':1h']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.xpath(".//*[@id=':16r']/div/div[2]"))).perform();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    driver.findElement(By.xpath(".//a[@title='Other Contacts (16)']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    //This is the page which is having the contact 
   List<WebElement> list = driver.findElements(By.xpath(".//*[@id=':17h']/tbody/tr/td[4]/div"));
    for(WebElement lst : list){
       //Trying to read the contact
        System.out.println(lst.getText());
    }//for
    }catch(Exception e){
        e.printStackTrace();
    }//catch
    finally{
        Thread.sleep(4000);
        driver.quit();
        System.exit(0);
    }//finally
  }//main
}//SortingTable

我正在尝试读取联系人页面中的数据,但遇到此异常。

共有1个答案

尉迟宣
2023-03-14

这是因为以下两个原因从这里:

在两种情况中的一种情况下会引发过时的元素引用异常,第一种情况比第二种情况更常见:

该元素已全部删除。该元素不再附加到DOM。

 类似资料: