laravellaravel-duskbrowser-testing

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


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')

Solution

  • You can use XPath:

    $selector = "//select[@name='category']/option[text()='Appliances']";
    $browser->driver->findElement(WebDriverBy::xpath($selector))->click();