问题
I got a select element that look like this, now I want to open it up and select the option with value t3, so I tried it like this:
<select id="selectMenu">
<option value=""> </option>
<option value="t1">test 1</option>
<option value="t2">test 2</option>
<option value="t3">test 3</option>
<option value="t4">test 4</option>
<option value="t5">test 5</option>
<option value="t6">test 6</option>
</select>
$this->byId('selectMenu')->click();
sleep(1);
$type = $this->elements($this->using('css selector')->value(option[value="t3"]'));
$type[0]->click();
Now this opens the menu but it does not select the option tag, I thought of using the select() instead of click() but select() is not supported yet, or atleast that's the message i get when I try to use the method. I run the following extension PHPUnit_Extensions_Selenium2TestCase.
回答1:
it's relatively simple.
$this->select($this->byId('selectMenu'))->selectOptionByValue('t3');
this should select the selectbox's option based on value. Using PHPUnit Selenium 2 v1.3.3
回答2:
I found I needed to convert the PHPUnit_Extensions_Selenium2TestCase_Element
to a PHPUnit_Extensions_Selenium2TestCase_Element_Select
:
PHPUnit_Extensions_Selenium2TestCase_Element_Select::fromElement($this->byId('selectMenu'))->selectOptionByValue('t3');
Also see this other question
回答3:
$this->byId('selectMenu')->selectOptionByValue('t3');
回答4:
To select using value use
$this->select($this->byName())->selectOptionByValue($value);
To select using label use
$this->select($this->byName())->selectOptionByLabel($label);
回答5:
You can try with
$this->select('css=#selectMenu','test 1');
来源:https://stackoverflow.com/questions/17168524/select-a-option-with-selenium2phpunit