使用 Selenium 通过 optgroup 选择 select 上的选项

发布于 2024-08-06 00:06:45 字数 892 浏览 4 评论 0原文

我正在尝试在选择元素中选择一个值。我正在使用 Selenium RC (Java) 来运行测试用例。据我所知,选择值的代码由以下给出:

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

我无法使用上述代码选择所需的值。我认为这可能与选择源代码中的 optgroup 有关。我没有收到任何异常,该命令执行正常,但查看页面时未选择所需的值。 另外,我不能使用 ids (而不是值),因为没有。下面是选择器的源代码:

<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>

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

如果我们能够避免使用选项值(例如“18”、“34”等),那就太好了,因为这些数字稍后会随着值的变化而变化。例如,“REQUIRED VALUE”的值为 -18,但如果我删除该项目并再次添加它,其值将会不同。基本上这个下拉框是动态的。

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")

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>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-08-13 00:06:45

您的示例中所需选项的值实际上是“18”。请尝试以下操作:

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

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

selenium.select("locator", "label=REQUIRED VALUE")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文