Selenium: How to choose an option from the selection menu?

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

I know how to enter something into the text box:

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

But how to choose an option from the drop-down menu?

+6
php selenium phpunit selenium-rc
source share
3 answers

To expand other (exact) answers, you can choose based on the label, value, identifier or index of the parameters. From the official certificate, available at http://release.seleniumhq.org/selenium-core/1.0/reference.html :

select (selectLocator, optionLocator)

Arguments:

  • selectLocator - element locator that defines the drop-down menu
  • optionLocator - parameter locator (default is a label)

Select an option from the drop-down list using the parameter locator.

Option locators provide various ways to specify the parameters of an HTML Select element (for example, to select a specific parameter or to claim that the selected option meets the specification). There are several forms of the Select Option locator.

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

If no parameter locator prefix is ​​specified, the default behavior must match the label.

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

All Articles