我在第四次测试中得到 StaleElementReferenceException 。我尝试使用显式等待来忽略异常,并等待该元素可点击,这在本论坛 ref:How to avoid "StaleElementReferenceException" in Selenium? 中的类似问题中提出。
public class AdminInterface {
@Test(priority=1)
public void adminToUserInterface() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.xpath("//a[text()='Online Catalog']")).click();
String str = "welcome";
String str1 = (driver.findElement(By.xpath("//div[@id='bodyContent']/div[1]/div[1]")).getText()).toLowerCase();
if(str1.contains(str))
System.out.println("Redirecting to user interface from admin interface successfull == PASS");
else
System.out.println("Redirecting to user interface from admin interface failed == FAILED");
Thread.sleep(2000);
driver.close();
}
@Test(priority=2)
public void loginTest() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
String str = "Catalog";
String str1=driver.findElement(By.xpath("//div[@id='adminAppMenu']/h3[1]/a")).getText();
if(str.equals(str1))
System.out.println("Login was successful==PASS");
else
System.out.println("Login was not successful==FAIL");
Thread.sleep(2000);
driver.close();
}
@Test(priority=3,dependsOnMethods={"loginTest"})
public void addManufacturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
driver.findElement(By.xpath("//span[text()='Insert']")).click();
driver.findElement(By.name("manufacturers_name")).sendKeys("AA1");
driver.findElement(By.id("tdb1")).click();
String str = "AA1";
List<WebElement> tds =driver.findElements(By.tagName("td"));
Iterator <WebElement> itr = tds.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
System.out.println("Manufacturer creation was successfull==PASS");
}
}
Thread.sleep(4000);
driver.close();
}
@Test(priority=4,dependsOnMethods={"addManufacturer"})
public void editManufacturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
String str = "AA1";
List<WebElement> lst = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr = lst.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
}
}
new WebDriverWait(driver,20).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='tdb2']/span[2]")));
driver.findElement(By.xpath("//a[@id='tdb2']/span[2]")).click();
driver.findElement(By.name("manufacturers_name")).sendKeys("AA2");
driver.findElement(By.id("tdb1")).click();
String str1="AA2";
List<WebElement> lst1 = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr1 = lst1.iterator();
while(itr1.hasNext())
{
int i =0;
if((itr1.next().getText()).equals(str1))
{
System.out.println("The manufacturer "+str+"'s name was successully changed to "+str1+"==PASS");
i++;
}
if(i==0)
{
System.out.println("The manufaturer "+str+"'s name could not be changed");
}
}
Thread.sleep(4000);
driver.close();
}
@Test(priority=5,dependsOnMethods={"editManufacturer"})
public void deleteManufaturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
String str = "AA2";
List<WebElement> lst = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr = lst.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
}
}
new WebDriverWait(driver,20).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Delete']")));
driver.findElement(By.xpath("//span[text()='Delete']")).click();
driver.findElement(By.id("tdb1")).click();
List<WebElement> lst1 = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr1= lst1.iterator();
int i=0;
while(itr1.hasNext())
{
if((itr1.next().getText()).equals(str))
{
System.out.println("Deleting the manufaturer "+str+" was unsuccessful == FAIL");
i=1;
}
}
if(i==0)
{
System.out.println("Deleting the manufacturer "+str+" was successful == PASS");
}
}
}
答案 0 :(得分:1)
而不是 -
driver.findElement(By.xpath("//a[@id='tdb2']/span[2]")).click();
尝试以下代码 -
element = driver.findElement(By.xpath("//a[@id='tdb2']/span[2]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
答案 1 :(得分:0)
伙计们我找到了这个问题的解决方案。在单击操作迭代所有可用元素之后,jvm被卡在while循环中。 A&#39; 休息 &#39;在找到元素时,必须使用statement来跳出循环。请参考以下代码:
原始代码:@Test 4
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
}
}
&#13;
更新的代码:
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
break;
}
}
&#13;
第5版TestScript也是如此。