Selenium:从下拉菜单中获取当前值

发布于 2024-08-19 14:54:51 字数 126 浏览 7 评论 0原文

我试图找到一个简单的 Selenium 调用来从选择下拉列表中获取当前选项。我知道有些调用会获取列表中的所有值,但我想知道当前选择了哪个选项。如果这是微不足道的,我很抱歉,但 google 和 Selenium IDE 没有帮助我。谢谢。

I'm trying to find a simple Selenium call to grab the current option from a select drop-down list. I'm aware there are calls which grab all the values in a list but I wish to know which option is currently selected. Apologies if this is trivial but google and Selenium IDE didn't help me. Thanks.

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

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

发布评论

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

评论(3

〃安静 2024-08-26 14:54:51

您应该能够使用 getSelected* 命令返回所选项目的 ID、索引或标签。下面引用自 Selenium 参考:


storeSelectedId ( selectLocator, variableName )
获取指定选择元素中选定选项的选项元素 ID。

参数:

  • selectLocator - 标识下拉菜单的元素定位器。variableName
  • - 要存储结果的变量的名称。

返回:指定选择下拉列表中选定的选项 ID


storeSelectedIndex ( selectLocator, variableName )
获取指定 select 元素中所选选项的选项索引(选项编号,从 0 开始)。

参数:

  • selectLocator - 标识下拉菜单的元素定位器。variableName
  • - 要存储结果的变量的名称。

返回:指定选择下拉列表中选定的选项索引


storeSelectedLabel ( selectLocator, variableName )
获取指定选择元素中选定选项的选项标签(可见文本)。

参数:

  • selectLocator - 标识下拉菜单的元素定位器。variableName
  • - 要存储结果的变量的名称。

返回:指定选择下拉列表中选定的选项标签

You should be able to use the getSelected* commands to return the ID, index, or label of the selected item. Below is quoted from the Selenium Reference:


storeSelectedId ( selectLocator, variableName )
Gets option element ID for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option ID in the specified select drop-down


storeSelectedIndex ( selectLocator, variableName )
Gets option index (option number, starting at 0) for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option index in the specified select drop-down


storeSelectedLabel ( selectLocator, variableName )
Gets option label (visible text) for selected option in the specified select element.

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • variableName - the name of a variable in which the result is to be stored.

Returns: the selected option label in the specified select drop-down

我很OK 2024-08-26 14:54:51

我会使用 storeSelectedValuegetSelectedValue

JUNIT

String value = selenium.getSelectedValue(selectLocator)

硒作用

storeSelectedValue ( selectLocator, variableName ) 

I would use storeSelectedValue or getSelectedValue

JUNIT

String value = selenium.getSelectedValue(selectLocator)

Selenium Action

storeSelectedValue ( selectLocator, variableName ) 
肤浅与狂妄 2024-08-26 14:54:51

有一个练习此类内容的链接:

https://letskodeit.teachable.com/p/practice< /a>”

有一个“选择类别示例”

1.在此测试中,首先单击下拉菜单中的“本田”
2.然后提取选择标签作为选项标签“Honda”的父级
3.然后将其转换为Select对象
4.然后我使用 getFirstSelectedOption() 将其与预期值“本田”进行比较。

    @Test
    public void selectTagDemo() {
        WebElement hondaElement = webDriver.findElement(By.xpath("//option[@value=\"honda\"]"));
        hondaElement.click();

        WebElement selectCarWebElement = hondaElement.findElement(By.xpath("//parent::select"));
        Select selectCar = new Select(selectCarWebElement);
        Assert.assertEquals(selectCar.getFirstSelectedOption().getText(), "Honda");
    }

如果您需要下面的整个测试类注释

there is a link to practice such things:

"https://letskodeit.teachable.com/p/practice"

there's a "Select Class Example"

1.in this test first it clicks on "Honda" in the dropdown menu
2. then extracts the select-tag as parent of the option-tag "Honda"
3. then converts it to Select object
4. then i uses the getFirstSelectedOption() to compare it with the expected value "Honda.

    @Test
    public void selectTagDemo() {
        WebElement hondaElement = webDriver.findElement(By.xpath("//option[@value=\"honda\"]"));
        hondaElement.click();

        WebElement selectCarWebElement = hondaElement.findElement(By.xpath("//parent::select"));
        Select selectCar = new Select(selectCarWebElement);
        Assert.assertEquals(selectCar.getFirstSelectedOption().getText(), "Honda");
    }

if you need the whole Test class comment below

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