gpt4 book ai didi

java - org.openqa.selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document

转载 作者:行者123 更新时间:2023-11-30 03:35:21 25 4
gpt4 key购买 nike

我正在尝试从 HTML 表中读取数据。我尝试读取数据的网站是 gmail 联系人页面。每次我运行代码时我都会得到 -

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)

我写的Java代码-

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

我正在尝试读取联系人页面内的数据,但出现此异常。

最佳答案

这是因为here中的以下两个原因:

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

The element has been deleted entirely. The element is no longer attached to the DOM.

这通常发生在基于 ajax 的网站中。解决这个问题的一种方法是重新获取元素,因为ajax调用DOM会更改

关于java - org.openqa.selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066135/

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