硒:如何从选择菜单中选择一个选项?

硒:如何从选择菜单中选择一个选项?

问题描述:

我正在使用PHPUnit Selenium扩展用PHP编写Selenium测试.

I am writing a Selenium test in PHP using the PHPUnit Selenium extension.

我知道如何在文本字段中输入内容:

I know how to type something into a text field:

$this->type('fieldName', 'value');

但是如何从下拉菜单中选择一个选项?

But how do I select an option from a drop-down menu?

要扩展其他(准确的)答案,可以根据选项的标签,值,id或索引进行选择.摘自 http://release.seleniumhq.org/selenium-core上的官方参考/1.0/reference.html :

To expand on the other (accurate) answers, you can select based on the label, value, id, or index of the options. From the official reference available at http://release.seleniumhq.org/selenium-core/1.0/reference.html:

选择(selectLocator,optionLocator)

参数:

  • selectLocator-标识下拉菜单的元素定位器
  • optionLocator-选项定位器(默认为标签)

使用选项定位器从下拉菜单中选择一个选项.

Select an option from a drop-down using an option locator.

选项定位器提供了不同的方式来指定HTML Select元素的选项(例如,用于选择特定选项或断言所选择的选项满足规范).选择选项定位器有几种形式.

Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.

  • 标签 = labelPattern :根据其标签(即可见文本)匹配选项. (这是默认设置.)
    • label = regexp:^ [Oo] ther
    • label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
      • label=regexp:^[Oo]ther
      • value = other
      • id = option1
      • index = 2

      如果未提供选项定位符前缀,则默认行为是在标签上匹配.

      If no option locator prefix is provided, the default behaviour is to match on label.