Sometimes Select option by Value doesnt work with Xpath

I am using Select option by value to select a batch from 2 batches available in the Select Batch dropdown. It has worked earlier for me in previous tc; but here its not slecting the batch from 2 batches available i.e 99 and B 005. What shd be changed to select batch B 005
.

I would not have the “= B 005” as part of the xpath. Instead, I would leave the xpath as:

//select[@id='selectedBatchNumberId']

And in the code I would use:

WebUI.selectOptionByValue(findTestObject('yourTO'), 'B 005', false)

And to confirm the option was selected:

WebUI.verifyOptionSelectedByValue(findTestObject('yourTO'), 'B 005', false, 10)
And the other:

To select the other option

WebUI.selectOptionByValue(findTestObject('yourTO'), '99', false)

And to confirm the other option was selected:

WebUI.verifyOptionSelectedByValue(findTestObject('yourTO'), '99', false, 10)

There are also:
WebUI.selectOptionByLabel(findTestObject('yourTO'), 'B 005', false)
and
WebUI.selectOptionByIndex(findTestObject('yourTO'), '3')

As @grylion54 alluded to, this is actually an invalid xpath:

//select[@id='selectedBatchNumberId']=B 005

This can be demonstrated in your browser console like so:

The appropriate xpath is, again as grylion has mentioned:

//select[@id='selectedBatchNumberId']

If you can share the HTML of the dropdown you are working with, we can confirm this 100%, but for now we are assuming that this xpath is correct.

The idea behind selectOptionByValue() is to:
1.) First, locate the actual <select> element for the dropdown. That is what your test object (and the xpath it defines) should do. Nothing more, nothing less.
2.) Match the value attribute for the <option> element you are trying to select.

For instance, if the HTML for your dropdown looked something like this:

<select id="selectedBatchNumberId">
    <option value="B 005">option_1</option>
    <option value="99">option_2</option>
    .
    .
    .
    <option value="xyz">option_n</option>
</select>

then this would work, as long as your test object has an appropriate xpath:

WebUI.selectOptionByValue(findTestObject('yourTO'), 'B 005', false)

Note: People often confuse selectOptionByValue() and selectOptionByLabel(). Here’s the difference:

  • selectOptionByValue matches the value attribute of the <option> element
  • selectOptionByLabel matches the text of the <option> element, i.e. the visible option when you open the dropdown

:smiling_face: Thanks to u all.