如何在selenium webdriver中选择UL下的列表项

时间:2016-02-02 09:45:09

标签: selenium-webdriver

下面是代码,我想选择第一个列表项i,e VAN。

<Div class="btn-group col-xs-12 noSidePad">
<button id="equipment-dropdown-btn" class="btn strip-radius btn-default dropdown-toggle col-xs-10 noSidePad" data-bind="html: selectedEquipment(), css: {'has-error' : $parent.errorMessage() && selectedEquipment() == 'Select...'}" aria-expanded="false" data-toggle="dropdown" type="button">Flatbed</button>
<button class="btn btn-default equipt-toggle dropdown-toggle col-xs-2 noSidePad" aria-expanded="false" data-toggle="dropdown" type="button">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul id="equipmentType" class="dropdown-menu" data-bind="foreach: equipmentTypeResults" role="menu">
<li>
<a data-bind="text: EquipmentName, click: $.proxy($parent.populateEquipment, $parent), attr: { 'data-itemtype': ResultType.toLowerCase(), 'data-equipid': EquipmentTypeId }" href="javascript:void(0);" tabindex="-1" role="menuitem" data-itemtype="parent" data-equipid="6">**Van**</a>

我被困了,我应该首先确定下拉字段,然后选择列表。

非常感谢您对此问题的任何帮助或指导。

2 个答案:

答案 0 :(得分:0)

首先尝试使用以下Xpath

//ul[@id='equipmentType']/li/a[contains(.,'**Van**')]

如果有一个具有此值的下拉列表,则您需要先点击该下拉列表

driver.findElement(YOUR Locator)).click();
List<WebElement> options = select.findElements(By.xpath("//ul[@id='equipmentType']/li"));
for (WebElement option : options) {
  if("**Van**".equals(option.getText()))
    option.click();
}

希望它会对你有所帮助:)。

答案 1 :(得分:0)

您需要先点击下拉按钮,您可以通过Xpath显示(或切换)选择列表

driver.findElement(By.Xpath(".//button[//span[text()='Toggle Dropdown']]")).click();

然后

driver.findElement(By.Xpath(".//ul[@id='equipmentType']//[text()='**Van*']")).click();

您可以使用其他值代替**Van**以使其更通用