缓存中找不到元素 - 自从查找以来,该页面已经发生了变化



我在第四次测试中获得了 staleelementReferenceException 。我尝试使用明确的等待来忽略异常,并等待该元素可单击,这在此论坛 ref中的一个类似问题中提出了建议:在硒?

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

}

而不是 -

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

我找到了这个问题的解决方案。点击操作迭代所有可用元素后,JVM被卡在While循环中。当找到元素时,必须使用A'' break '语句'语句。请参阅下面的代码:

原始代码:在@test 4

 while(itr.hasNext())
        {
            if((itr.next().getText()).equals(str))
                    {
                   itr.next().click();
                    }
        }

更新的代码:

 while(itr.hasNext())
        {
            if((itr.next().getText()).equals(str))
                    {
                   itr.next().click();
                   break;
                    }
        }

第五次测试标题也是如此。

相关内容

最新更新