我正在尝试从数据列表中进行选择,但只有列表中的第一个元素可以选择。
这是一个HTML代码段:
<td>
<input id="applianceFilterTextbox" class="flat" name="applianceFilter" list="applianceNames" value="ROC-1006 - B827EBB5D539" style="width: 100%"/>
<datalist id="applianceNames">
<!-- ngRepeat: app in appliances -->
<option class="ng-binding ng-scope" ng-repeat="app in appliances" value="ROC-1006 - B827EBB5D539" ng-value="app.DisplayName">ROC-1006 - B827EBB5D539</option>
<!-- end ngRepeat: app in appliances -->
<option class="ng-binding ng-scope" ng-repeat="app in appliances" value="ROC-1007 - B827EBD15125" ng-value="app.DisplayName">ROC-1007 - B827EBD15125</option>
<!-- end ngRepeat: app in appliances -->
<option class="ng-binding ng-scope" ng-repeat="app in appliances" value="ROC-1008 - B827EB05DEF3" ng-value="app.DisplayName">ROC-1008 - B827EB05DEF3</option>
<!-- end ngRepeat: app in appliances -->
<option class="ng-binding ng-scope" ng-repeat="app in appliances" value="ROC-1009 - B827EB2A379C" ng-value="app.DisplayName">ROC-1009 - B827EB2A379C</option>
<!-- end ngRepeat: app in appliances -->
</datalist>
</td>
我的模块如下:
public void SelectApplianceFromDatalist(int index)
{
ExplicitWait.waitElementToBeClickable(driver, 25, appliancesFilterTextBox);
appliancesFilterTextBox.Clear();
appliancesFilterTextBox.Click();
string select1 = driver.FindElement(By.XPath("//*[@id='applianceNames']/option['" + index + "']")).GetAttribute("value");
index++;
string select2 = driver.FindElement(By.XPath("//*[@id='applianceNames']/option['" + index + "']")).GetAttribute("value");
index++;
string select3 = driver.FindElement(By.XPath("//*[@id='applianceNames']/option['" + index + "']")).GetAttribute("value");
index++;
string select4 = driver.FindElement(By.XPath("//*[@id='applianceNames']/option['" + index + "']")).GetAttribute("value");
appliancesFilterTextBox.SendKeys(driver.FindElement(By.XPath("//*[@id='applianceNames']/option['"+ index +"']")).GetAttribute("value"));
}
select1,select2,select3和select4仅用于调试目的。当为模块调用例如3的索引时,它们都包含第一个选项的值。
答案 0 :(得分:1)
尝试使用简单方法sendKeys()
发送您所需的值,例如 -
driver.findElement(By.id("applianceFilterTextbox")).clear();
driver.findElement(By.id("applianceFilterTextbox")).sendKeys("ROC-1008 - B827EB05DEF3");
它将清除默认选择的值并输入您需要的值。
其他的事情是,我认为你正在使用正确的approch,但你的XPath
存在轻微的错误。改变
By.XPath("//*[@id='applianceNames']/option['" + index + "']")
到
By.XPath("//*[@id='applianceNames']/option["+ index +"]")