XPath : Finding GUI element by matching multiple text(s) pattern of a attribute value

Guest Author

Recently i had to spend my whole day searching for ways to find a elements' XPath (a ajax based button) by matching multiple text pattern within a single attribute value. I was using Selenium IDE to automate testing of Sun Portal Server GUI which is based on Ajax.

My Requirement : Uniquely Identify a ajax based button having the below XPath


My Problem : Attribute(@id) value has both constant and dynamic texts which makes it difficult to uniquely identify the element based on its value

If you look at my ajax button's xpath , there is a numeral "81533739" which is not constant and it changes everytime but the first part(ccd_ccd_) and the second part(_removeevent) of the @id value is constant . I want to uniquely identify @id attribute based on these constant text alone

Solution:The below xpath will uniquely identify my element using concat() and contains() XPath methods

//a[contains(@id,concat("ccd","_","ccd","_")) and contains(@id,"removeevent")]

Below are few links which i found helpful for this solution:

Join the discussion

Comments ( 3 )
  • Phil Fearon Wednesday, March 5, 2008

    Good result. If I understand your requirement the following will also serve by comparing the start and end of the id attribute:

    //a[starts-with(@id, "ccd_ccd_") and substring(@id, string-length(@id) - 10, 11) = "removeevent"]

    This expression looks more complex/awkward because there's no 'ends-with' function in XPath 1.0.

    Instead, a substring is required first to get the end value. The first substring argument is the target string value, the second the start position, and the third the length of the sub-string to extract.

    Not pretty, but it works:-)

  • Thekkadath Wednesday, March 5, 2008

    Thanks for that comment phil.

    wondering why xpath doesnt allow ends-with function although it already has a starts-with :)

  • Phil Fearon Wednesday, March 5, 2008

    Good question. The W3C XPath guys must also have asked that - because there is an 'ends-with' function in XPath 2.0 :-)

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.