" org.openqa.selenium.StaleElementReferenceException:元素不再附加到DOM"当我在两个方法中使用隐式等待时

时间:2016-10-27 07:34:54

标签: java selenium selenium-webdriver

当我在两个方法上使用隐式等待时,它会抛出" StaleElementReferenceException"在执行第一个方法并在第一个方法之后停止执行但是当我使用隐式等待方法时,两个方法都被执行(但是在控制台上打印错误但不影响执行)。

错误和代码的附加屏幕截图。我是selenium和java的新手,请帮我解决。

public static WebElement prepProduct(WebDriver driver)
            throws InterruptedException {
            WebDriverWait wait = new WebDriverWait(driver, 500);

            Boolean enable = driver.findElement(
                    By.xpath(".//*[@id='batch-update-prep-owner']")).isEnabled();
            if (enable.TRUE) {

                Select whoPrepsMerchant = new Select(driver.findElement(By
                        .xpath(".//*[@id='prep-items']/tr[2]/td[5]/select")));
                whoPrepsMerchant.selectByIndex(new Random()
                        .nextInt(whoPrepsMerchant.getOptions().size()));
                wait.until(ExpectedConditions.presenceOfElementLocated((By
                        .id("fba-inbound-manifest-workflow-view-manifest-prep-items-controller"))));
                String merchant = whoPrepsMerchant.getFirstSelectedOption()
                    .getText();
            String whoPrepsvalue = (driver.findElement(By
                    .xpath(".//*[@id='prep-items']/tr[2]/td[7]/span")))
                    .getText();
            if (merchant.equalsIgnoreCase("Amazon")) {
                if (!whoPrepsvalue.equalsIgnoreCase("--")) {
                    driver.findElement(By.xpath(".//*[@id='continue-plan']"))
                            .click();
                }
            } else {
                if (whoPrepsvalue.equalsIgnoreCase("--")) {
                    driver.findElement(By.xpath(".//*[@id='continue-plan']"))
                            .click();
                }
            }
        } else {
            driver.findElement(By.xpath(".//*[@id='continue-plan']")).click();
        }
        return element;
    }

    public static void prepLabel(WebDriver driver) throws IOException{
        WebDriverWait waitLabel = new WebDriverWait(driver, 500);
        try{
        Boolean enable = driver.findElement(
                By.xpath(".//*[@id='batch-update-label-owner']")).isEnabled();
        if (enable.TRUE) {
            Select whoLabels = new Select(driver.findElement(By
                    .xpath(".//*[@id='label-items']/tr[2]/td[5]/select")));
            whoLabels.selectByIndex(new Random().nextInt(whoLabels.getOptions()
                    .size()));
            waitLabel.until(ExpectedConditions.presenceOfElementLocated(By.id("fba-inbound-manifest-workflow-view-manifest-label-items-controller")));
            String merchant = whoLabels.getFirstSelectedOption().getText();

            String whoLabelsCost = (driver.findElement(By
                    .xpath(".//*[@id='label-items']/tr[2]/td[7]/span")))
                    .getText();
            if (merchant.equalsIgnoreCase("Amazon")) {
                if (!whoLabelsCost.equalsIgnoreCase("--")) {

                    driver.findElement(By.xpath(".//*[@id='continue-plan']"))
                            .click();
                }
            } else {
                if (whoLabelsCost.equalsIgnoreCase("--")) {

                    driver.findElement(By.xpath(".//*[@id='continue-plan']"))
                            .click();
                }
            }
        }
        else {
            driver.findElement(By.xpath(".//*[@id='continue-plan']")).click();
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            driver.findElement(By.xpath(".//*[@id='continue-plan']")).click();
        }
        }

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找选择下拉元素。所以改变你的代码

String merchant = whoPrepsMerchant.getFirstSelectedOption().getText();

String merchant = whoPrepsMerchant.selectByVisibleText("YourText");

String merchant = whoPrepsMerchant.selectByIndex(indexValue);// 0 or something like that

String merchant = whoPrepsMerchant.selectByValue("YourValue");