简体   繁体   English

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

[英]Selenium: How to select an option from a select menu?

I am writing a Selenium test in PHP using the PHPUnit Selenium extension. 我正在使用PHPUnit Selenium扩展在PHP中编写Selenium测试。

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? 但是如何从下拉菜单中选择一个选项?

To expand on the other (accurate) answers, you can select based on the label, value, id, or index of the options. 要扩展其他(准确)答案,您可以根据选项的标签,值,ID或索引进行选择。 From the official reference available at http://release.seleniumhq.org/selenium-core/1.0/reference.html : 来自http://release.seleniumhq.org/selenium-core/1.0/reference.html上的官方参考:

select(selectLocator, optionLocator) select(selectLocator,optionLocator)

Arguments: 参数:

  • selectLocator - an element locator identifying a drop-down menu selectLocator - 标识下拉菜单的元素定位器
  • optionLocator - an option locator (a label by default) optionLocator - 选项定位器(默认情况下为标签)

Select an option from a drop-down using an option locator. 使用选项定位器从下拉列表中选择一个选项。

Option locators provide different ways of specifying options of an HTML Select element (eg for selecting a specific option, or for asserting that the selected option satisfies a specification). 选项定位器提供了指定HTML Select元素选项的不同方法(例如,用于选择特定选项,或用于断言所选选项满足规范)。 There are several forms of Select Option Locator. 选择选项定位器有多种形式。

  • label = labelPattern : matches options based on their labels, ie the visible text. label = labelPattern :根据标签匹配选项,即可见文本。 (This is the default.) (这是默认设置。)
    • label=regexp:^[Oo]ther 标签=正则表达式:^ [乌]疗法
  • value = valuePattern : matches options based on their values. value = valuePattern :根据值的值匹配选项。
    • value=other 值=其它
  • id = id : matches options based on their ids. id = id :根据其ID匹配选项。
    • id=option1 ID =选项
  • index = index : matches an option based on its index (offset from zero). index = index :根据索引(从零开始偏移)匹配一个选项。
    • index=2 索引= 2

If no option locator prefix is provided, the default behaviour is to match on label. 如果未提供选项定位器前缀,则默认行为是在标签上匹配。

//note that it's the option text not value
$this->select('selectName', 'LabelText');
 $this->select("selectFieldIdentifier", "label=Option label");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM