问题
<select>
<option value=''>-- Select an Option --</option>
@foreach ($options as $option)
<option value='{{ $option->value }}'>{{ $option->name }}</option>
@endforeach
</select>
Select the first dynamic option
回答1:
$option = $I->grabTextFrom('select option:nth-child(2)');
$I->selectOption("select", $option);
$I->click("Submit");
回答2:
I've run into the same issue quite often when starting out with Codeception. Using the recommended answer, I created a helper function in my AcceptanceTester class to make this a little easier.
public function selectFromDropdown($selector, $n)
{
$option = $this->grabTextFrom($selector . ' option:nth-child(' . $n . ')');
$this->selectOption($selector, $option);
}
Where $n
is the position in the option list.
Then all you have to do is call it like this:
$I->selectFromDropdown('select', 1);
This has been working for me on pages that have several select's that load their option list based on the selected option of the previous select.
回答3:
sorry i do not have submit button, in my case i have to select the dropdown element and somehow need to tell the codeception to finish the selection.At the moment i can select but that select is not visible as i suppose the selection is not finished somehow.Below is my code to select the element.
$I->selectOption('//*[@class="ng-scope" and @ng-controller="dataIsland"]/*[local- name()="select"]','partlycloudy');
来源:https://stackoverflow.com/questions/17413985/select-an-option-in-a-dynamic-select-with-codeception