Selenium StaleElementReferenceException显式等待

时间:2015-07-13 09:31:40

标签: java selenium selenium-webdriver

在我的Selenium测试代码中,我有几行

  1. 点击复选框
  2. 从选择框中选择项目
  3. 点击按钮提交表单
  4. 这是代码

    WebElement selectAllElement = driver.findElement(By.id("filterForm:selectAll"));
    if (selectAllElement.isSelected())
    {
        selectAllElement.click();
    }
    
    Select selectLocation = new Select(new WebDriverWait(driver, 30)
        .until(ExpectedConditions
        .presenceOfElementLocated(By.id("filterForm:selectLocation"))));
    
    selectLocation.selectByVisibleText(location);
    
    WebElement filterButton = driver.findElement(By.id("filterForm:filterButton"));
    filterButton.click();
    

    我在尝试检索页面上的Select元素时收到StaleElementReferenceException,试图解决这个问题,我在这个元素上添加了一个明确的等待,如上面的代码所示。

    但我仍然得到StaleElementReferenceException

    编辑(这些元素的HTML)

    <form id="filterForm" name="filterForm" method="post" action="/UsersJSFMavenApplication/faces/index.xhtml" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="filterForm" value="filterForm" />
    <input id="filterForm:selectAll" type="checkbox" name="filterForm:selectAll" checked="checked" title="allUsers" onclick="mojarra.ab(this,event,'valueChange',0,'filterForm:filterGrid usersForm')" />All users<table id="filterForm:filterGrid">
    <tbody>
    <tr>
    <td><input id="filterForm:userName" type="text" name="filterForm:userName" disabled="disabled" /></td>
    <td><select id="filterForm:selectLocation" name="filterForm:selectLocation" size="1" disabled="disabled">   <option value="Keynsham">Keynsham</option>
        <option value="London">London</option>
        <option value="Silicon Valley">Silicon Valley</option>
    </select></td>
    <td><input id="filterForm:filterButton" type="submit" name="filterForm:filterButton" value="Filter" disabled="disabled" /></td>
    </tr>
    </tbody>
    </table>
    <input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-8198231560613594227:-8434387391056535341" autocomplete="off" />
    </form>
    

5 个答案:

答案 0 :(得分:2)

几周前我遇到了类似的问题。阅读StaleElementException首先阅读的内容可能是一个好主意。

我的问题是在选择元素和执行某些操作之间进行异步调用。我的解决方案是在while循环中包装try catch并尝试在抛出异常之前多次单击元素。像这样:

     public static void clickElement(By by) throws Exception {
        boolean isClicked = false;
        int attempts = 0;
        while(!isClicked && attempts < 5) {
            try {
                WebElement element = driver().findElement(by);
                element.click();
                isClicked = true;
            } catch(StaleElementReferenceException e) {
                attempts++;
                System.out.println("[DEBUG] StaleElementReference exception caught, trying to locate and click element again");
            }
        }
        if(!isClicked) {
          throw new Exception("Could not click " + by + ", after 5 attempts");
        }
     }

答案 1 :(得分:0)

如果未选中或未启用该复选框,则可以单击复选框。

尝试这样:

  WebElement checkboxEle = driver.findElement(By.id("filterForm:selectAll"));
  if (!checkboxEle.isEnable())
   {
        checkboxEle.click();
   }

  //To Select option
 WebElement selectElement = driver.findElement(By.xpath("//select[@name='filterForm:selectLocation']"));
 Select selectLocation = new Select(selectElement);

 selectLocation.selectByVisibleText("London");

答案 2 :(得分:0)

我设法通过更改

解决了这个问题
Select selectLocation = new Select(new WebDriverWait(driver, 30)
    .until(ExpectedConditions
    .presenceOfElementLocated(By.id("filterForm:selectLocation"))));

Select selectLocation = new Select(new WebDriverWait(driver, 30)
    .until(ExpectedConditions
    .elementToBeClickable(By.id("filterForm:selectLocation"))));

为什么它有所作为我不知道,也许有人可以作为评论发表一些亮点。

答案 3 :(得分:0)

当元素不再附加到DOM时,我们通常会得到陈旧元素异常。因此,如果找不到所需的元素,请尝试刷新页面。

page.driver.browser.send(:bridge).http.instance_variable_get(:@http).read_timeout=

答案 4 :(得分:-1)

driver.manage().timeOuts().implicitWait(timeUnit.SECONDS,10);