这就是 HTML 的样子。
<select name="ctl00$bodyContent$ddl_Territory" onchange="javascript:setTimeout('__doPostBack(\'ctl00$bodyContent$ddl_Territory\',\'\')', 0)" id="ctl00_bodyContent_ddl_Territory" class="select2-container" style="margin-left: 3%;">
<option selected="selected" value=""></option>
<option value="675">ALASKA AK (OCRGA061A) - HCP</option>
<option value="271">ALBANY GA (OCBDB041A) - HCP</option>
<option value="125">ALBANY NY (OCBAA031A) - HCP</option>
<option value="126">ALBANY NY (OCBAA032A) - HCP</option>
<option value="426">ALBANY NY (OCRAA031A) - HCP</option>
....
<option value="427">ALBANY NY (OCRAA031A) - HOSPITAL</option>
不断失败的自动化代码 -
Select territory = new Select(driver.findElement(By.name("ctl00$bodyContent$ddl_Territory")));
System.out.println("selected");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
// Trying to figure if sleep is required or not..
e.printStackTrace();
}
List<WebElement> optionList = territory.getOptions();
for (WebElement webElement : optionList) {
System.out.println(webElement.getText()); //this print nothing..empty line..confused
}
territory.selectByIndex(1); //exception thrown here
当我调用 selectByIndex
时,我得到 org.openqa.selenium.ElementNotInteractableException:
。我也尝试过 selectByValue 等,但没有帮助。 我确信 territory
指向正确的元素,因为 optionList 计数与 HTML 中的选项数量相同。但是不确定为什么文本没有在循环中打印以及为什么会出现异常..
驱动程序 - geckodriver-v0.18.0-win64
TIA!
更新1:webElement.isDisplayed()
为所有选项打印false
。 getLocation()
打印 X 和 Y 坐标的 0,0
..
最佳答案
获取所有选项的替代方法如下
List<WebElement> optins = driver.findElements(By.xpath("//select[@name='ctl00$bodyContent$ddl_Territory']/options"));
通过可见文本选择的替代方法
driver.findElement(By.name("ctl00$bodyContent$ddl_Territory")).sendKeys("ALBANY NY (OCBAA031A) - HCP");
关于java - Selenium : org. openqa.selenium.ElementNotInteractableException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45295567/