Java 显示“在缓存中未找到元素 - 可能页面自查找后已更改”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22348216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:06:55  来源:igfitidea点击:

"Element not found in the cache - perhaps the page has changed since it was looked up" displayed

javaseleniumselenium-webdriverelementbrowser-cache

提问by user3388456

"Element not found in the cache - perhaps the page has changed since it was looked up" when I logout and try to login in the same page.

当我注销并尝试在同一页面中登录时,“在缓存中找不到元素 - 可能页面自查找以来已更改”。

In the above case, login to application is successful, and when I logout from application, the login page is displayed again. Problem is: when I tried to login again in same page, it shows Element not found in the cache - perhaps the page has changed since it was looked up

在上述情况下,登录应用程序成功,当我退出应用程序时,再次显示登录页面。问题是:当我尝试在同一页面中再次登录时,它显示在缓存中找不到元素 - 也许该页面自查找以来已更改

Login Url (fresh, 1st time): //firco/en_US/d Logput URL (where login page is displayed again): //firco/en_US/logout/

Login Url (fresh, 1st time): //firco/en_US/d Logput URL (再次显示登录页面): //firco/en_US/logout/

I want to use same driver (browser instance) for 1st and 2nd login.

我想对第一次和第二次登录使用相同的驱动程序(浏览器实例)。

public static void main(String[] args) 
WebDriver driver = new FirefoxDriver();
driver.get("//Continuity/en_US/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement uname = driver.findElement(By.xpath(".//*[@id='text-input-element-15']"));
WebElement pwd = driver.findElement(By.xpath("id('text-input-element-16')"));
WebElement busunit = driver.findElement(By.xpath("id('text-input-element-22')"));
WebElement login = driver.findElement(By.id("login-button"));
uname.sendKeys("RAGHU");
pwd.sendKeys("Hello00");
login.click();
WebElement LogoutButton = driver.findElement(By.xpath(".//*[@id='logout-button']"));
LogoutButton.click();
driver.get("//Continuity/en_US/");
uname.sendKeys("SUGU");

In above code I want to user uname at both 1st and 2nd login (after logout), in same driver

在上面的代码中,我想在第一次和第二次登录时(注销后)在同一个驱动程序中使用 uname

回答by Sitam Jana

Web elements you stored before clicking on login button will not be present in cache after login because of page refresh or page changes. You need to again store these web elements in order to make them available again under cache. I have modified your code a bit which might help:

由于页面刷新或页面更改,您在单击登录按钮之前存储的 Web 元素在登录后不会出现在缓存中。您需要再次存储这些 Web 元素,以使它们在缓存下再次可用。我稍微修改了您的代码,这可能会有所帮助:

public static void main(String[] args) 
WebDriver driver = new FirefoxDriver();
driver.get("//Continuity/en_US/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement uname = driver.findElement(By.xpath(".//*[@id='text-input-element-15']"));
WebElement pwd = driver.findElement(By.xpath("id('text-input-element-16')"));
WebElement busunit = driver.findElement(By.xpath("id('text-input-element-22')"));
WebElement login = driver.findElement(By.id("login-button"));
uname.sendKeys("RAGHU");
pwd.sendKeys("Hello00");
login.click();
WebElement LogoutButton = driver.findElement(By.xpath(".//*[@id='logout-button']"));
LogoutButton.click();
driver.get("//Continuity/en_US/");
uname = driver.findElement(By.xpath(".//*[@id='text-input-element-15']"));
uname.sendKeys("SUGU");