6

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?

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 1
    Little trick: you can always record your test - or part of - using Selenium IDE (Firefox extension) and convert recorded test to PHP using File -> Export test case as ... -> PHP Selenium RC – ts. May 12 '10 at 22:18

3 Answers3

12

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:

select(selectLocator, optionLocator)

Arguments:

  • selectLocator - an element locator identifying a drop-down menu
  • optionLocator - an option locator (a label by default)

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

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.

  • label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
    • label=regexp:^[Oo]ther
  • value=valuePattern: matches options based on their values.
    • value=other
  • id=id: matches options based on their ids.
    • id=option1
  • index=index: matches an option based on its index (offset from zero).
    • index=2

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

Dave Hunt
  • 8,191
  • 4
  • 38
  • 39
2
//note that it's the option text not value
$this->select('selectName', 'LabelText');
halfer
  • 19,824
  • 17
  • 99
  • 186
Andrew
  • 227,796
  • 193
  • 515
  • 708
2
 $this->select("selectFieldIdentifier", "label=Option label");
ts.
  • 10,510
  • 7
  • 47
  • 73