在selenium-webdriver中定位select list的方法比较简单,用id和name等属性可以很方便的将select给找出来,但是怎么去选择下拉框中的某一项呢?
思路是这样的,首先定位到select list元素,然后找出该select list下所有的option,点击该option element既可,以下面的html代码为例
<html>
<head>
<title>Select</title>
</head>
<body>
<span>select demo</span>
<select id =
"s"
name =
"ns"
>
<option value =
"0"
>Op1</option>
<option value =
"1"
>Op2</option>
<option value =
"2"
>Op3</option>
<option value =
"3"
>Op4</option>
</select>
</body>
</html>
通过下面的代码可以选择到下拉框中的第2个option,也就是text为Op2的选项
不难看出这样的代码还是不太直观的。为了能够更好的操作select元素,我们可以对其做一个简单的封装,示例代码如下:
通过引用该文件,我们的代码此时就应该是如下所示:
通过上面的代码可以看出,在选择了下拉框的某1个option时,如果下拉框上绑定有onchange事件,那么onchange事件是不会被触发的,这应该是上面解决方案的一个缺陷。
另外EasyWrap由于简单起见并没有支持选择多个option时的情况,好在选择多个option的情况一般不太多见,所以EasyWrap还是有一点实用价值的。