硒:如何从选择菜单中选择一个选项? [英] Selenium: How to select an option from a select menu?

查看:77
本文介绍了硒:如何从选择菜单中选择一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

      这篇关于硒:如何从选择菜单中选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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