2016-02-28 73 views
2

选择项目我有这个drop down list从下拉列表

<div class="select_id" style="width: 592px;"><span class="left"></span><span class="center">103</span><a class="select-opener"></a></div> 
<select class="id-hidden" name="ids"><option value="">Please select...</option> 
<option value="a">a</option> 
<option value="b">b</option> 
<option value="c">c</option> 
<option value="d">d</option> 
<option value="e">e</option> 

所以我尽量选择项目。

所以我有这个元素:

val selectAccountDropDownListElement: WebElement = 
    wait.until(ExpectedConditions.visibilityOfElementLocated(
    By.cssSelector(("div.select_id")))) 

打开下拉列表:

selectAccountDropDownListElement.click() 

现在我怎样才能从我drop down list选择一个项目?

回答

0

这是因为(docs)一样简单:

singleSel("id").value = "a" 

或者:

singleSel("id").selection = Some("a") 
+0

什么是singleSel? –

+0

@davidhol它是一个扩展Element的类([docs](http://www.artima.com/docs-scalatest-2.0.M5/org/scalatest/selenium/WebBrowser$SingleSel.html))。 – alecxe

+0

如何使用它? (我没有finf的例子) –

0

你也许可以找到的元素的XPATH。

例如,(请查一下),但:

a_xpath = "/select/option[0]" 
b_xpath = "/select/option[1]" 
c_xpath = "/select/option[2]" 
d_xpath = "/select/option[3]" 
e_xpath = "/select/option[4]" 

(我建议使用Firefox的扩展Firebug很容易找到的XPath)

然后,你可以点击之后你的信选项打开下拉列表:

selectAccountDropDownListElement.click() # opens dropdown box 
driver.find_element_by_xpath(a_xpath).click() # clicks 'a' dropdown option 

以下是一些可以帮助您的指南。 :)

http://selenium-python.readthedocs.org/locating-elements.html http://www.wikihow.com/Find-XPath-Using-Firebug

+0

我想避免使用索引号导致列表更改,所以我更喜欢按名称使用项目 –

+0

尝试:'a_xpath =“//输入[@值='a']”'或甚至是'/ select/option [。 ='a']'(来自http://stackoverflow.com/questions/1535377/xpath-for-selecting-option-html-tag) – Brandon

+0

我想我不能达到这个,因为select元素是隐藏的,请参阅html示例 –