Select 基于 drop-down 和 Laravel Dusk 中文本的选项

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

我想根据文本选择一个drop-down选项,我应该怎么做? 这是我的 Html 代码:

<select name="category">
   <option>Appliances</option>
   <option>Sporting Goods</option>
   <option>Cosmetics</option>
</select>

这是我在 dusk 中的选择器:

->select('category','Appliances')

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:

因此请为您的 select 字段赋值。

<select name="category">
   <option value="Appliances">Appliances</option>
   <option value="Sporting Goods">Sporting Goods</option>
   <option value="Cosmetics">Cosmetics</option>
</select>

然后使用

->select('category','Appliances')

您可以使用 XPath:

$selector = "//select[@name='category']/option[text()='Appliances']";
$browser->driver->findElement(WebDriverBy::xpath($selector))->click();
<select class="form-select" name="product_category_id_productList_add" id="product_category_id_productList_add">
     <option value="" disabled selected>-- Select Product Category --</option>
       @foreach($product_category_data as $data)
        <option value="{{$data->id}}">{{$data->name}}</option>
       @endforeach
</select>
->select('product_category_id_productList_add', '11')