Select an option based on text in drop-down with Laravel Dusk

こ雲淡風輕ζ 提交于 2019-12-08 00:31:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!