【问题标题】:Selenium Webdriver Stale Element Exception errorSelenium Webdriver 陈旧元素异常错误
【发布时间】:2015-11-26 11:44:50
【问题描述】:

您好,我正在使用 selenium webdriver,并且在运行以下脚本时,我在脚本中突出显示的位置遇到过时元素异常错误。

我的脚本停在那里,我无法运行。

错误消息是 - Stale Element 异常错误。在缓存中找不到该元素,可能页面在查找后已更改。

任何人请在这种情况下帮助我并指导我纠正这个错误。

问候, 维涅什 K S

@Test
  public void testClient() throws Exception {
    driver.get("http://t1accounts.govreports.com.au/?service=govreports");
    driver.findElement(By.id("Password")).clear();
    driver.findElement(By.id("Password")).sendKeys("Viki2607");
    driver.findElement(By.id("UserName")).clear();
    driver.findElement(By.id("UserName")).sendKeys("vignesh@eimpact.com.au");
    driver.findElement(By.id("btnLogin")).click();
    driver.findElement(By.xpath(".//*[@id='side-menu']/li[2]/a/span")).click();
    driver.findElement(By.cssSelector("span.hide380")).click();
    driver.findElement(By.id("Individual")).click();
    //driver.wait(5);
    **driver.findElement(By.id("ABN")).clear();**
    driver.findElement(By.id("ABN")).sendKeys("38091241128");
    driver.findElement(By.id("TFN")).clear();
    driver.findElement(By.id("TFN")).sendKeys("100000001");
    driver.findElement(By.id("BusinessName")).clear();
    driver.findElement(By.id("BusinessName")).sendKeys("LORGE CONSULTING (AUSTRALIA) PTY LTD");
    driver.findElement(By.id("TradingName")).clear();
    driver.findElement(By.id("TradingName")).sendKeys("LORGE CONSULTING (AUSTRALIA) PTY LTD");
    //driver.findElement(By.xpath("//ul[@id='Individual_Salutation_listbox']/li")).click();
    driver.findElement(By.id("Individual_Salutation")).clear();
    driver.findElement(By.id("Individual_Salutation")).sendKeys("Ms");
    //new Select(driver.findElement(By.id("Individual_Salutation"))).selectByVisibleText("Mr");
    //driver.findElement(By.id("Individual_Salutation")).sendKeys("Mr");
    driver.findElement(By.id("Individual_FirstName")).clear();
    driver.findElement(By.id("Individual_FirstName")).sendKeys("Joan");
    driver.findElement(By.id("Individual_LastName")).clear();
    driver.findElement(By.id("Individual_LastName")).sendKeys("Ignatius");
    driver.findElement(By.id("Individual_Phone")).clear();
    driver.findElement(By.id("Individual_Phone")).sendKeys("042323155");
    driver.findElement(By.id("Individual_Email")).clear();
    driver.findElement(By.id("Individual_Email")).sendKeys("test@govreports.com.au");
    driver.findElement(By.id("PostalAddress_Line1")).clear();
    driver.findElement(By.id("PostalAddress_Line1")).sendKeys("Walker Street");
    driver.findElement(By.id("PostalAddress_City")).sendKeys("SYDNEY");
    driver.findElement(By.id("PostalAddress_Region")).sendKeys("NSW");
    driver.findElement(By.id("PostalAddress_Postcode")).sendKeys("1001");
    Thread.sleep(5000);
    driver.findElement(By.id("PostalAddress_Country")).sendKeys("Australia");
    //Select objSelect = new Select(null);
    //new Select(driver.findElement(By.id("PostalAddress_City"))).selectByVisibleText("SYDNEY, NSW, Australia, 1001");
    driver.findElement(By.id("saveClient")).click();
    Thread.sleep(5000);
  }

【问题讨论】:

  • 您在哪一行遇到异常?
  • 你为什么还要这样发送数据?这是硬编码,您应该将测试数据保存在单独的存储库中(excel、数据库),您的测试数据本质上应该是动态的。

标签: java selenium webdriver


【解决方案1】:

工作正常,由于应用程序非常慢,您可能需要添加 3-5 秒的隐式等待。 或者,在 .//*[@id='side-menu']/li[2]/a/span 元素处,您也可以执行

driver.navigate().to("http://t1hub.govreports.com.au/App/#/Clients/MyClients");

我相信您遇到问题的地方是ABN。但是,如果诱导适当的等待,它会很好。

【讨论】:

  • 嗨.. 非常感谢.. 它对我有用.. 我已经纠正了过时的异常错误.. 但我面临另一个问题,我无法选择下拉菜单值..在上面的脚本中有一个地址字段..这些字段不是自动完成字段..我必须单独从下拉列表中选择这些值..我不知道如何访问下拉值..请帮帮我..
  • 这是一个不同的问题...如果它回答了这个问题,请接受这个答案,并为您的新问题开始另一个问题。
【解决方案2】:

当发生过时元素异常时!!

当支持这些文本框/按钮/链接的库发生更改时,可能会发生陈旧元素异常,这意味着元素相同,但网站中的引用现在已更改,而不会影响定位器。因此,我们存储在缓存中的引用(包括库引用)现在已经过时或陈旧,因为页面已被更新的库刷新。

for(int j=0; j<5;j++)
try {
    WebElement elementName=driver.findElement(By.id(“Individual_Salutation”));
    break;
} catch(StaleElementReferenceException e){
e.toString();
System.out.println(“Stale element error, trying ::  ” + e.getMessage());
}

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多