1

I've the following html and I want to select third option which is Off (optional) using selenium PHP-webdriver. Can anyone please tell me how can I do this?

In this HTML, all ids, are dynamically generated. So that I can not use id to find an element e.g I can not use this:

$driver->findElement(WebDriverBy::id('ajax-item-ExchangeEmail-12345'));

Can we use cssSelector() or xpath()? If yes, then how?

Thanks.

<div id="ajax-item-12345" class="input-group" title="ExchangeEmail">
    <label class="input-group-addon" for="ajax-item-ExchangeEmail-12345">ExchangeEmail</label>
    <select id="ajax-item-ExchangeEmail-12345" class="form-control" name="category_resource[12345]">
        <option value="2">On (mandatory)</option>
        <option value="1">On (optional)</option>
        <option value="0">Off (optional)</option>
        <option value="3">Off (mandatory)</option>
    </select>
</div>
techsu
  • 753
  • 2
  • 7
  • 11

2 Answers2

1

Using cssSelector() and selectByVisibleText() functions, I can now choose any option that I want. Thanks.

with(new WebDriverSelect($driver->findElement(WebDriverBy::cssSelector('div[title="ExchangeEmail"] select'))))
        ->selectByVisibleText('Off (optional)');
techsu
  • 753
  • 2
  • 7
  • 11
1

The selectByVisibleText() command can be used to select the list option from the drop-down field using label text:

Example,to use CSS selector:

$selectDiv = WebDriverBy::cssSelector('div[title="ExchangeEmail"] select');
$selectElement = new WebDriverSelect($driver->findElement($selectDiv));
$selectElement->selectByVisibleText('Off (optional)');

Example,to use XPath:

$sel = $driver->findElements(WebDriverBy::xpath('//select'));
$select = new WebDriverSelect($sel);
$select->selectByVisibleText('Off (optional)');
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64