Forum Discussion

obaid_shirwani's avatar
obaid_shirwani
Contributor
11 years ago

Getting null object in response of xPath

Hi guys,



I am trying the following:




//Set page to be the existing page: Works good.

var page = Sys.Browser("*").Page("*");




//The following is working good and object is getting identified.  

var tempObj = page.FindChildByXPath("//input[@id='ddlMonth']");


  if(tempObj != null)


  {


    tempObj.Click();


    //tempObj.Text = 'JAN';


  }

 


  


  Sys.Refresh();


 

//This is the PROBLEM AREA. Here, tempObj gets a null object. The xPath is valid.



tempObj = page.EvaluateXPath("/html/body/div[6]/div/div[5]");


  if(tempObj != null)


  {


    tempObj.Click();


  }





What am I doing wrong?


  

  • EvaluateXPath returns VBScript-compatible arrays. To use the returned array in JScript, you need to convert it to a native JScript array like this:



    var tmp = page.EvaluateXPath(...);

    if (tmp != null)

    {

      var arr = (new VBArray(tmp)).toArray();

      ...

    }



    This is mentioned in the EvaluateXPath documentation.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    EvaluateXPath returns VBScript-compatible arrays. To use the returned array in JScript, you need to convert it to a native JScript array like this:



    var tmp = page.EvaluateXPath(...);

    if (tmp != null)

    {

      var arr = (new VBArray(tmp)).toArray();

      ...

    }



    This is mentioned in the EvaluateXPath documentation.
  • Resolved: I did the following and it started working: 



    tempObj = page.FindChildByXPath("//div[6]/div/div[5]");







    But: I am still confused as to what I was doing wrong in the first approach?
  • The first approach i.e.   tempObj = page.EvaluateXPath("//div[6]/div/div[5]"); is getting an array. View the attached image: ElementsScreenshot02.png



    I tried doing the following:




    if(tempObj != null)


      {


        tempObj[0].Click();


      }



    but it returned an exception that ''0' is null or not an object'