程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Selenium StaleElementReferenceException 显式等待

发布于2021-11-28 10:29     阅读(218)     评论(0)     点赞(0)     收藏(1)


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

  1. 单击复选框
  2. 从选择框中选择一个项目
  3. 单击按钮提交表单

这是代码

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

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

然而,我仍然得到一个 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>

解决方案


我已经设法通过更改解决了这个问题

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

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



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/337841/7e282dc0c34012fe1346/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)