首页 > 解决方案 > Selenium/Java - 无法找到带有索引的选项

问题描述

我正在使用 practiceautomation.com 网站练习 selenium。我在注册时遇到问题 - 我的选择器无法找到带有索引的选项。

代码:

Select yearSelector = new Select(driver.findElement(By.id("years")));
            yearSelector.selectByIndex(2000);

我得到了类似的东西:

org.openqa.selenium.NoSuchElementException: Cannot locate option with index: 2000
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'DESKTOP-NN5LV43', ip: '192.168.0.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.1'
Driver info: driver.version: unknown

这很奇怪,因为我也使用

Select dateSelector = new Select(driver.findElement(By.id("days")));
        dateSelector.selectByIndex(15);

一切正常,通常选择列表中的日期

图片: 年份 如您所见,年份是可见的。

标签: javaselenium

解决方案


您尝试按索引2000进行选择,但错误提示没有索引 2000 的选项。也许您想按值选择?

Select yearSelector = new Select(driver.findElement(By.id("years")));
yearSelector.selectByValue("2000");

推荐阅读