Eitanos30
Eitanos30

Reputation: 1439

XPath for element by attribute value in Selenium?

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?

Upvotes: 1

Views: 1464

Answers (3)

kjhughes
kjhughes

Reputation: 111726

Change

//option[value='0']

to

//option[@value='0']

because value is an attribute, not an element.

Upvotes: 2

AakashM
AakashM

Reputation: 63378

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

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193308

//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?

Upvotes: 3

Related Questions