问题
I want to choose a drop-down option based on its text,what should i do? This is my Html code :
<select name="category">
<option>Appliances</option>
<option>Sporting Goods</option>
<option>Cosmetics</option>
</select>
And this is my selector in dusk :
->select('category','Appliances')
回答1:
You can use XPath:
$selector = "//select[@name='category']/option[text()='Appliances']";
$browser->driver->findElement(WebDriverBy::xpath($selector))->click();
回答2:
To select a value in a dropdown selection box, you may use the select method. Like the type method, the select method does not require a full CSS selector. When passing a value to the select method, you should pass the underlying option value instead of the display text:
So give a value to your select fields.
<select name="category">
<option value="Appliances">Appliances</option>
<option value="Sporting Goods">Sporting Goods</option>
<option value="Cosmetics">Cosmetics</option>
</select>
Then use
->select('category','Appliances')
来源:https://stackoverflow.com/questions/51082201/select-an-option-based-on-text-in-drop-down-with-laravel-dusk