1

For the following HTML:

enter image description here

Why does the following XPath doesn't work:

//option[value='0']

Is it related to value or to option element?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Eitanos30
  • 1,331
  • 11
  • 19

3 Answers3

3
//option[value='0']

is not a valid selector incase you are attempting to identify/select the respective <option> element using Selenium.


Solution

You can use either of the Locator Strategies:

  • xpath:

    //option[@value='0']
    
  • css_selector:

    option[value='0']
    

tl; dr

Why should I ever use CSS selectors as opposed to XPath for automated testing?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
2

Change

//option[value='0']

to

//option[@value='0']

because value is an attribute, not an element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

Your current xpath is searching for an option element with a child element value which in turn has contents 0. What you actually want to find is an option element with an attribute value with value 0:

//option[@value='0']
kjhughes
  • 106,133
  • 27
  • 181
  • 240
AakashM
  • 62,551
  • 17
  • 151
  • 186
  • I thought that in order to search for a child i should use **/** – Eitanos30 Feb 03 '22 at 16:42
  • @Eitanos30: AakashM (+1) (and all answerers, in fact) have explained that `value` selects a child ***element***; `@value` selects an ***attribute***. Your markup has `value` as an attribute, so use `@`. – kjhughes Feb 03 '22 at 16:57