Home » Php » Click an option in dropdown menu using PHP Selenium Webdriver?

Click an option in dropdown menu using PHP Selenium Webdriver?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am using PHP Selenium Webdriver wrapper by Facebook. Can anyone please give me an example of how to click or select an option from a select drop down menu?

I have tried this:

$test = $driver->findElement( WebDriverBy::id('drop1').WebDriverBy::cssSelector("option[value='11']"));
$test->‌​click();

but it errors out:

Message : Object of class WebDriverBy could not be converted to string

How to&Answers:

Try the below code.

$test = $driver->findElement("css selector", 'select[id="drop1"] option[value='11']');
$test->‌​click();

Answer:

It should be

$test = $driver->findElement( WebDriverBy::id('drop1') )
               ->findElement( WebDriverBy::cssSelector("option[value='11']") )
               ->click();

If you are working on “select” tag, use WebDriverSelect instead.

$select = new WebDriverSelect($driver->findElement(WebDriverBy::id('drop1')));
$select->selectByValue('11');

Answer:

Do you want click or select item?
If select than follow: Facebook framework helper

Works like:

$selectingContainer = $driver->findElement("locator");

$selection = new WebDriverSelect($selectingContainer);

$selection->selectByVisibleText($text);

"locator" – it is locator for dropdown menu element.

Answer:

This worked for me, hope it helps someone 🙂

findElement( WebDriverBy::cssSelector(
                        'div#sku-grid.grid-view table.items thead tr.filters td 
                         select[name="Pro[exception_type]"] option[value="inRetail"]'
                         ));`