How to search node by exact text match using Xpath in webdriver

79,838

Solution 1

I would use next xpath //button[text()='abc']. You have mentioned text() function but I'm not sure syntax was correct. Also you tried to use contains() -- it searches partial text and WebDriver gets first element found. I your case it is <button>abcd</button> button

Solution 2

To find the element 'abcd' you can simply use:

//button[contains(text(),'abcd')]

To find 'abc' use the normalize-space() function which will clean up your text for comparison purposes.

//button[normalize-space(text())='abc']

Solution 3

//button[.="abc"]

The dot before the equality operator will do the text comparison. Another example is /PROJECT[.="MyProject"] from the xPath Java tutorial.

Solution 4

For exact search:

button[text()='abc']

For pattern matching search:

button[starts-with(.,'abc')]
Share:
79,838
souvik
Author by

souvik

Updated on October 23, 2020

Comments

  • souvik
    souvik over 3 years

    I need a little help regarding searching an exact text using xpath in webDriver.

    Suppose i have the html as follows..

    <html><body>
      <table>
        <tr>
          <td><button>abcd</button></td>
          <td><button>abc</button></td>
        </tr>
      </table>
    </body></html>
    

    Now i want to click button "abc"

    I used xpath as //button[contains(text(),'abc')] but it is always performing on button "abcd" as it also contain the text "abc". In this regards I need a predicate or some other procedure which can search exact text instead of containing text.

    I also tried with //button[matches(text(),'abc')], //button[matches($string,'abc')], //button[Text='abc')], //button[.='abc')] and many more but none of these worked out to identify "abc" button.

    I do not know if there is any problem regarding my xpath version as I'm not aware of the version. But I'm using java 1.6 JDK.

    Though my exact scenario is not the example shown but similar logic needs to be applied.

    Hence any help or suggestion would be highly appreciated.

  • souvik
    souvik over 10 years
    thanx for ur comment, tell me one thing, the html contains leading and trailing space for the text. like " abcd " and " abc ".if I use [contains(text(),'abc')] xpath then its performing on "abcd" button. but if i'm using [starts-with(text(),' abc') or [ends-with(text(),'abc '), its showing unable to find locator.the space i mentioned in xpath leading or trailing to the string is correct??And secondly the "td" number is not fixed in my case. the button may contain in any td in the table. infact in some other tr tag also as the button generation is dynamic. Hence I can't go by hierarchy.
  • Akbar
    Akbar over 10 years
    Cannot tell without accessing the application but you can do it the round about way by getting text attribute value and do a string comparison using the language functions.
  • Akbar
    Akbar over 10 years
    Element.getattribute("text")
  • souvik
    souvik over 10 years
    thanx for the suggestion. It would be better if i had a predicate to match for an exact text. though it was a bit hectic anyway i made the library using string checking by Element.getText() method and now its working fine...
  • Ross Patterson
    Ross Patterson over 10 years
    @souvik The normalize-space() function will clean up your text for comparison purposes. Try //button[normalize-space(text())='abc']
  • Akbar
    Akbar over 10 years
    It is commom to accept or upvote if your problem is resolved.
  • MansoorShaikh
    MansoorShaikh over 7 years
    Works like a charm. Thanks.
  • Tarmac
    Tarmac about 4 years
    normalize-space example worked for me on Selenium IDE for chrome