In my Selenium test code I have a few lines that
- Clicks a check box
- Selects an item from a select box
- Clicks a buttom to submit a form
Here is the code
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();
I was receiving a StaleElementReferenceException when trying to retrieve the Select element on the page, to try and get around this I added an explicit wait on this element as can be seen in the code above.
I am however still getting a StaleElementReferenceException
EDIT (HTML for those elements)
<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>