java - 无法在 Select with Selenium 中选择选项
问题描述
当我尝试<select>
在 Selenium 中选择一个选项时遇到问题。
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
这简直给了我ElementClickIntercepted
. 试图点击它也给了我ElementClickIntercepted
。尝试用 JS 点击它会给我一个NullPointerException
. 我可以使用元素选择器在 Firefox 中轻松选择它,因此选择顶部没有任何东西阻止我单击它。
什么是拦截点击?通常是因为一个元素覆盖了另一个元素,它会在测试结果中告诉我,但这里没有。
<div class="pull-left">
<select name="nb" class="form-control">
<option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100000">All</option>
</select>
</div>
选择 xPath:
//select[@name="nb"]
它是页面上唯一的选择。
解决方案
由于元素是<select>
理想的元素,因此您需要使用Select类。要调用click()
值为1000的选项,您需要诱导WebDriverWait并且elementToBeClickable()
您可以使用以下任一 Locator Strategies:
cssSelector
:new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control[name='nb']")))).selectByValue("100000");
xpath
:new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='form-control' and @name='nb']")))).selectByValue("100000");
推荐阅读
- java - 如何在具有自定义标签的 xml 中添加新行?
- javascript - 选择事件侦听器在 forEach 循环中不起作用
- php - PHP数据库更新中如何使用SQL累计
- python-3.x - 如果我不点击 google chrome 图标,Driver.get("url") 会抛出超时错误
- php - laravel 如何自动加载类并在不启动它的情况下使用它们?
- python - 连续按4次后几秒不播放音效?
- reactjs - 我在合同中返回一个数组。如何使用 react 和 web3 在我的 app.js 中接受它?
- python-3.x - DataFrame:我如何按 Z 分组并计算 Y 范围内的平均 X
- sql - 查找用于将财政季度结果与上一年进行比较的查询
- css - -webkit-transform:rotateY 在 Mac OS 上失败