Forum Discussion

han's avatar
han
Contributor
13 years ago

xPath on text

Having problem using the text of the html element to grep on in an EvaluateXPath.



I have tried the following (and they work in Chrome Developer Tools ):



  • //*[text()='Text']

  • //*[contains(.,'Text')]

  • //*[.='Text']

  • //*[text()[contains(.,'Text')]]


How should the xPath be written to work in TC?

  • Hi Håkan,



    I've reproduced the issue and filed a bug report for the developers to look into. Thanks for bringing this to our attention.



    As a workaround, you can use the Page.FindChild, Page.FindAllChildren or Page.NativeWebObject.Find method for the search. For example, instead of the //*[text()='Text'] XPath search, you can use page.FindAllChildren("contentText", "*Text*", 100).



    However, if you need a patch for this, please open a support ticket and refer to this thread.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Håkan,



    These expressions should work fine in TestComplete as well.



    Could you please clarify which problems you are having? Posting complete script code (or keyword test) and the URL or source code of your web page will help in getting your problem solved. Also, please let me know your TestComplete version (as in Help > About) and your browser version.
  • han's avatar
    han
    Contributor

    function test(){

      var page = Sys.Browser("chrome").Page("http://smartbear.com/");

      var item = page.EvaluateXPath("//a[text()]");

    }



    In chrome Developer tools the same xPath returs 141 nodes and in TC 0 (or undefined)
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Håkan,



    I've reproduced the issue and filed a bug report for the developers to look into. Thanks for bringing this to our attention.



    As a workaround, you can use the Page.FindChild, Page.FindAllChildren or Page.NativeWebObject.Find method for the search. For example, instead of the //*[text()='Text'] XPath search, you can use page.FindAllChildren("contentText", "*Text*", 100).



    However, if you need a patch for this, please open a support ticket and refer to this thread.

    • william_roe's avatar
      william_roe
      Super Contributor

      Has this been fixed or does the workaround still need to be used?

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Hi william_roe,

         

        This specific bug (XPath text search worked in Internet Explorer but not in Chrome/Firefox) was fixed in TestComplete 9.20 back in 2012.

         

        If you're having problems with XPath, please post a code example, your browser version and TestComplete version, and we'll look into it.