【问题标题】:"Element not found in the cache - perhaps the page has changed since it was looked up" displayed显示“在缓存中未找到元素 - 页面可能在查找后已更改”
【发布时间】:2014-04-16 09:25:09
【问题描述】:

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

在上述情况下,登录应用程序成功,当我从应用程序注销时,再次显示登录页面。问题是:当我尝试在同一页面中再次登录时,它显示 Element not found in cache - 也许页面在查找后已更改

登录网址(新鲜,第一次)://firco/en_US/ d Logput URL(再次显示登录页面)://firco/en_US/logout/

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

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");

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

【问题讨论】:

  • 这是因为退出后页面刷新了。您需要在登录元素上再次findElement
  • 请分享您的代码
  • Anand S ...我已添加我的代码供您参考..

标签: java selenium selenium-webdriver element browser-cache


【解决方案1】:

由于页面刷新或页面更改,您在单击登录按钮之前存储的 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");

【讨论】:

  • 当元素在同一个驱动程序中再次被识别时,它解决了。但是注销后会显示一个模式对话框并再次加载url
  • 我不知道再次获取元素的值是否是一种好习惯,但它确实有效。谢谢!
  • 很好解释。有这方面的文件吗? @mfsi
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多