Search code examples
seleniumselenium-webdriverxpathwebdriver

How to find element using the value attribute with Selenium and XPath


In selenium I used the following XPATH to find all elements which have the word ok:

//button[(((@value='ok')) or ((@value='Ok')) or ((@value='OK')))]

What if I want to find all buttons which start with the word ok? As an example OK1 is a match.


Solution

  • To find all elements which have the word ok you can modify as follows:

    //button[@value='ok' or @value='Ok' or @value='OK']
    

    and to find all elements which starts with the word ok you can use:

    //button[starts-with(@value, 'ok') or starts-with(@value, 'Ok') or starts-with(@value, 'OK')]