【问题标题】:How to get the selected value from a combobox using Selenium WebDriver (Selenium 2)?如何使用 Selenium WebDriver(Selenium 2)从组合框中获取选定的值?
【发布时间】:2012-08-16 16:54:32
【问题描述】:

假设我有这个 html 代码:

<select id="superior" size="1" name="superior">
    <option value=""></option>
    <option value="c.i.e.m.md.Division_1">DIVISION007</option>
    <option selected="selected" value="c.i.e.m.md.Division_$$_javassist_162_119">MyDivision</option>
    <option value="c.i.e.m.md.Division_121">MyDivision4</option>
    <option value="c.i.e.m.md.Division_122">MyDivision5</option>
</select>

所以这是一个组合框

id=superior 

并且当前值 MyDivision 被选中。

使用 Selenium WebDriver 我试图获取选定的值,但没有成功。

我试过了:

String option = this.ebtamTester.firefox.findElement(By.id(superiorId)).getText();
return option;

但这会返回组合框中的所有值。

请帮忙?

编辑:

WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
String wantedText = selectedValue.getValue();

【问题讨论】:

  • 您使用什么语言?爪哇?

标签: selenium-webdriver


【解决方案1】:

这是用 C# 编写的,但将其转换为您正在使用的任何其他语言应该不难:

IWebElement comboBox = driver.FindElement(By.Id("superior"));
SelectElement selectedValue = new SelectElement(comboBox);
string wantedText = selectedValue.SelectedOption.Text;

SelectElement 要求您使用 OpenQA.Selenium.Support.UI,因此在顶部键入

using OpenQA.Selenium.Support.UI;

编辑:

我想你会使用“驱动程序”而不是“驱动程序”

IWebElement comboBox = this.ebtamTester.firefox.FindElement(By.Id("superior"));

【讨论】:

  • 我收到一条错误消息,提示 SelectElement 构造函数受保护,因此我不能创建它的实例。
  • 您是用 C# 还是其他语言编写的?另外,你能用你现在正在测试的代码编辑你的主要帖子吗?
  • 您好,感谢您的帮助。我正在使用 JAVA。我编辑了我的第一篇文章。 new SelectElement() 不起作用,因为正如我所说,构造函数似乎受到保护。当我尝试时它起作用: Select selectedValue = new Select(comboBox);但是,我不知道如何获取当前选定的文本?
  • 你好,我让它像这样工作:WebElement comboBox = ebtamTester.firefox.findElement(By.id("superior"));选择 selectedValue = new Select(comboBox); String WantedText = selectedValue.getFirstSelectedOption().getText(); ebtamTester.logger.logText("选择的值来自" + selectBoxName, WantText);谢谢你..
  • selectedValue.SelectedOption.Text;将为您提供所选项目的文本。有谁知道如何获得选定的值。我想测试该值是否符合我的预期,而不是可以更改的文本。
【解决方案2】:

在 Java 中,以下代码应该可以正常工作:

import org.openqa.selenium.support.ui.Select;
Select comboBox = new Select(driver.findElement(By.id("superior")));
String selectedComboValue = comboBox.getFirstSelectedOption().getText();
System.out.println("Selected combo value: " + selectedComboValue);

由于当前选择了MyDivision,上面的代码会打印“MyDivision”

【讨论】:

  • 感谢您的回答。你的回答对我帮助很大。
【解决方案3】:

selectedValue.SelectedOption.Text;会给你文本 选定的项目。有谁知道如何获取选定的值。

要获取选定的值,请使用

selectedValue.SelectedOption.GetAttribute("value");

【讨论】:

    【解决方案4】:

    根据标签选择选项:

    Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
    select.deselectAll();
    select.selectByVisibleText("Value1");
    

    获取第一个选择的值:

    WebElement option = select.getFirstSelectedOption()
    

    【讨论】:

    • 我尝试了 option.toString() 并且我得到了:[[[[Firefox: UNIX 上的 firefox (a88d1b9f-93d8-4140-9693-5a91d59b57f6)] -> id: Superior]] -> 标签名称:选项]
    • 原来我需要使用.getText。谢谢。
    【解决方案5】:

    在 C# 中使用 XPath

      string selectedValue=driver.FindElement(By.Id("superior")).FindElements(By.XPath("./option[@selected]"))[0].Text;
    

    【讨论】:

      【解决方案6】:

      根据@Micheal 的回答,使用以下命令会更容易:

      string  selectedValue = new SelectElement(driver.FindElement(By.Id("Year"))).SelectedOption.Text;
      

      【讨论】:

      • 感谢您改进 an existing answer。如果您认为您的信息作为新答案比对现有答案的评论更好,我建议edit 以确保您的答案是完整/完整的,并且不需要阅读不同的答案并将其与您的新信息相结合。通常,我还会对该答案发表评论,以提及我发布了改进的答案。
      • 在这种情况下,我相信这与现有答案几乎完全相同,但变量内联到一行(尽管字符串不正确 "Year" 而不是 "superior")和更少信息。我建议为此目的使用 cmets 而不是答案。
      猜你喜欢
      • 2015-01-22
      • 2014-10-20
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多