出现错误:org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档

时间:2019-12-04 07:01:17

标签: java xml selenium selenium-webdriver testng

当我第一次尝试从下拉菜单中选择选项时,它起作用了,但是第二次我收到错误消息,因为陈旧的元素引用:元素未附加到页面文档中 我尝试给等待仍然得到同样的错误。 发布代码时出现错误,因此我发布了代码图片

enter image description here

```
@Test(priority = 15)
    // To check Edit partner feature by Selecting "Food Type" one by one option
    public void editPartnerBySelectingFoodType() throws InterruptedException {

        // Click on Partners
        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[3]/a")).click();

        // Click on All Partners
        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[3]/ul/li[2]/a")).click();

        // Search Partner
        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
        driver.findElement(By.xpath("//*[@id=\"bootstrap-data-table_filter\"]/label/input")).sendKeys("NH37 Dhaba");

        // Click on edit button
        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
        driver.findElement(By.xpath("//*[@id=\"bootstrap-data-table\"]/tbody/tr/td[8]/center/a[1]/img")).click();

        String[] foodType = {"Select Food Type", "veg", "veg-non veg", "Pure veg"};

        //Created variable for Food Type dropdown
        WebElement selectFoodType = driver.findElement(By.xpath("//select[@id='foodtype']"));



         Thread.sleep(3000);
        //Created variable for dropdown option
            List<WebElement> optionLinks = driver.findElements(By.xpath("//*[@id=\"foodtype\"]/option"));
            System.out.println(optionLinks.size()); 

            for(int i=0; i<optionLinks.size(); i++) {



                String name =optionLinks.get(i).getText();

                //get array list
                List<String> foodTypeOptions = Arrays.asList(foodType);
                if(foodTypeOptions.contains(name)) {

                    System.out.println(name);

                    wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                    driver.findElements(By.xpath("//*[@id=\"foodtype\"]/option")).get(i).click();
                    Thread.sleep(2000);

                      // Click on Update to update
                    driver.findElement(By.xpath("//*[@id=\"marchant_form\"]/div[22]/button[1]")).click();

                    alert = driver.switchTo().alert();

                    wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                    if (alert.getText().equals(PartnerAlert2)) {

                        System.out.println(alert.getText());
                        alert.accept();

                        // Click on Partners
                        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[3]/a")).click();

                        // Click on All Partners
                        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                        driver.findElement(By.xpath("//*[@id=\"main-menu\"]/ul/li[3]/ul/li[2]/a")).click();

                        // Search Partner
                        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                        driver.findElement(By.xpath("//*[@id=\"bootstrap-data-table_filter\"]/label/input")).sendKeys("NH37 Dhaba");

                        // Click on edit button
                        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                        driver.findElement(By.xpath("//*[@id=\"bootstrap-data-table\"]/tbody/tr/td[8]/center/a[1]/img")).click();

//                      wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
//                      //Click on Food Type drop down
//                       selectFoodType.click();
//                       
                        wait = new WebDriverWait(driver, 5 /* timeout in seconds */);
                        driver.findElement(By.xpath("//*[@id=\"foodtype\"]")).click();


                    }
                }

            }

```

0 个答案:

没有答案