如何使用selenium web driver for java获取特定optgroup下的选项? [英] how to get options under a particular optgroup using selenium web driver for java?

查看:120
本文介绍了如何使用selenium web driver for java获取特定optgroup下的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从多选选项列表字段中选择特定optgroup下的选项,例如

I want to select options under particular optgroup from a multiselect picklist field, for example

<select id="xyz">
   <optgroup label="Group1">
      <option value="1">pick1</option>
      <option value="2">pick2</option>
   </optgroup>
   <optgroup label="Group2">
       <option value="3">pick3</option>
       <option value="4">pick4</option>
   </optgroup>
</select>

如果选项列表字段如上所示,我可以使用

if the picklist field is like above, I can use

new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick1");
new Select(driver.findElement(By.id("xyz"))).selectByVisibleText("pick2"); 

从上面选择optgroup -Group1下的所有选项。

for selecting all the options which is under optgroup -Group1 from the above.

在这里,我知道Group1中存在的所有选项,因此我可以选择如上所述。

Here I know which are all the options present in the Group1 and hence I can select like above.

但我的要求是动态的,我需要选择optgroup-Group1下的所有选项。

But my requirement is dynamically I need to select all the options under optgroup-Group1.

如果我想要选择所有选项,包括第1组和第1组。 2动态

If I want to select all the options both including Group 1 & 2 dynamically

然后我可以使用

new Select(driver.findElement(By.id("xyz"))).getOptions();

将所有选项作为List获取,我可以将其存储为数组,我可以选择全部选项一个接一个地使用数组

to get all options as a List and I can store it as an array, and I can select all the Options one by one using array

但是如何才能在一个特定的optgroup下获得所有选项,比如Group1或Group2?

But how can I get all the options under only one specific optgroup say Group1 or Group2?

推荐答案

你应该使用 xpath findElements

driver.findElements(By.xpath("xpath = "//select[@id='xyz']/optgroup[@label='Group1']/option")))

这篇关于如何使用selenium web driver for java获取特定optgroup下的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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