嗨,我正在使用selenium网络驱动程序,在运行下面的脚本时,我在脚本中突出显示的位置遇到了过时元素异常错误。
我的脚本停在那里,我无法运行。
错误消息为-Stale Element异常错误。在缓存中找不到该元素——也许自查找该页面以来,该页面已发生更改。
在这种情况下,任何人都请帮助我,并指导我纠正这个错误。
谨致问候,Vignesh 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);
}
工作正常,您可能需要添加3-5秒的隐式等待,因为应用程序非常慢。或者,在元素.//*[@id='side-menu']/li[2]/a/span
处,您也可以进行
driver.navigate().to("http://t1hub.govreports.com.au/App/#/Clients/MyClients");
我相信你遇到问题的地方是ABN
。然而,如果诱导适当的等待,效果会很好。
发生失效元素异常时!!
当支持这些文本框/按钮/链接的库发生变化时,可能会发生过时的元素异常,这意味着元素是相同的,但现在网站中的引用发生了变化,而不会影响定位器。因此,我们存储在缓存中的引用(包括库引用)现在已经变旧或过时,因为页面已经用更新的库刷新。
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());
}