使用Selenium在带有optgroup的select上选择选项 [英] Using Selenium for selecting an option on a select with optgroup

查看:152
本文介绍了使用Selenium在带有optgroup的select上选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在select元素中选择一个值.我正在使用Selenium RC(Java)运行测试用例.我了解选择值的代码由以下代码给出:

I'm trying to select a value in a select element. I'm using Selenium RC (Java) to run the test cases. I understand that the code for selecting a value is given by:

selenium.select("locator", "value=REQUIRED VALUE")

我无法使用上面的代码选择所需的值.我认为这可能与选择源代码中的optgroup有关.我没有任何异常,该命令执行得很好,但是在页面上未选择所需的值. 另外,我不能使用ID(而不是值),因为那里没有任何ID.这是选择器的源代码:

I'm unable to select the desired value with the above code. I think it might be something to do with optgroup in the select source code. I do not get any exceptions, the command executes fine but looking at the page the required value is not selected. Also, I cant use ids (instead of value) because there arent any. Here is the source code of the selector:

<select>
   <optgroup label="Group1">
      <option value="13">some value1</option>
      <option value="25">some value2</option>
   </optgroup>
   <optgroup label="Group2">
      <option value="18">REQUIRED VALUE</option>
      <option value="34">some value3</option>
      ...
      ...
   </optgroup>
</select>

是否可以使用Selenium选择所需的值?

Is there any way to select the required value using Selenium?

如果我们可以避免使用选项值(例如"18","34"等),那就太好了,因为这些数字后来会随着值的改变而改变.例如,"REQUIRED VALUE"的值为-18,但如果我删除该项目并再次添加,则其值将有所不同.基本上,此下拉框是动态的.

It would be great if we could avoid the option values (such as "18", "34" etc) because these numbers change later as the values change. For example, "REQUIRED VALUE" has a value -18 but if I were to delete this item and add it again its value would be different. Basically this drop-down box is dynamic.

推荐答案

示例中所需选项的值实际上是'18'.请尝试以下操作:

The value for the required option in your example is actually '18'. Try the following:

selenium.select("locator", "label=REQUIRED VALUE")

这篇关于使用Selenium在带有optgroup的select上选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆