3

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')
Mahsa kia-rad
  • 59
  • 1
  • 6

3 Answers3

3

You can use XPath:

$selector = "//select[@name='category']/option[text()='Appliances']";
$browser->driver->findElement(WebDriverBy::xpath($selector))->click();
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
1

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')
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
-1
<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')
  • Thank you for your answer but it seems like the thread owner doesn't have control about how the select is created. – shaedrich Mar 31 '21 at 08:29