Forum Discussion

ArmandsBruns's avatar
ArmandsBruns
Frequent Contributor
10 years ago

XPath expression

Hi,



I am trying to use EvaluateXPath method to find the objects and how is possible to get faster XPath, specially for CHROME:



This Xpath expressions doesn't work:

Set page = Sys.Browser("*").Page("*")

Set obj= page.EvaluateXPath("//DIV[@id='SAD.htm.0']/TABLE/TBODY/TR[3]/TD[2]", False)



This expressions works:

Set page = Sys.Browser("*").Page("*")

Set obj = page.FindChildByXPath("//SPAN[@id='SAD.htm.0']", False)



How can I use [TD], [TR] in XPath expression?



Reagrds,

Armands





5 Replies

  • Hi Armands,



    Try to change "//DIV[@id='SAD.htm.0']" to "//DIV[@id='PDFS2117434']". It seems to me that it should work when you fix it.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Armands,

     


    The EvaluateXPath method returns an array. Thus, you shouldn't use the Set statement for obj.


     


    I've created the following simple script that clicks one of the links on the http://smartbear.com/forums/ page (I'm using TestComplete 10.30):




    Sub Test


      Dim arr, page


      Set page = Sys.Browser("chrome").Page("http://smartbear.com/forums/")


      


      arr = page.EvaluateXPath("//*[@id='frm_28']/table/tbody/tr[2]/td[2]/div/span[1]/a")


     


      If Not IsNull(arr) Then


        arr(0).Click()


      End If 


     


    End Sub




     


    How does this code work for you?


     

  • ArmandsBruns's avatar
    ArmandsBruns
    Frequent Contributor
    I have attached an example the way the page is coded 'inspect element'

    How can I call marked object?



  • ArmandsBruns's avatar
    ArmandsBruns
    Frequent Contributor
    Hi Andrey,



    Thanks for your response.



    It was just an example.

    I tried different options but unsuccessfully.



    Set page = Sys.Browser("*").Page("*")

    Set obj = page.EvaluateXPath("//DIV[@id='PDFS2117434']//TABLE[@class='octa-min']//TR/TR/TR/TD/TD", False)



    Set obj = page.EvaluateXPath("//DIV[@id='PDFS2117434']//TABLE[@class='octa-min']//TR[3]/TD[2]", False)



    Regards

    Armands
  • Goobledy's avatar
    Goobledy
    Occasional Contributor
    Hi Armands,



    Alternatively, you can try using class name of table as given below:



    obj= page.EvaluateXPath(“//TABLE[@class=‘oct-min']/TABLE/TBODY/TR[3]/TD[2]", False)