Forum Discussion

Al2's avatar
Al2
Occasional Contributor
5 years ago
Solved

Unable to click on an object when using GetElementsByClassName

Hi,

 

I have a small piece of code here as a sampl of what I'm trying to do. But it fails.

 

function RelVis(){
var page = browser.CH;
//this return an array of all the object found with the following criteria 
var buttonRel = page.contentDocument.getElementsByClassName("btn acute-btn rel");

//Log.Message(buttonRel.length);

    if(buttonRel[0].Exists){ 
      buttonRel[0].Click();
    }
  }

Just FYI, the lenght of this array is 1. I've checked it a few times while debugging it.

But what i get is this.  I'd really appreciate it anyone can point out the problem here. 

 

 

Thank you

Al2

 

  • What's the contents of that one item in the array?  While it's true that the array may have a single element, it could be that it's a null value or simply an empty element.  The GetElementsByClassName is not a TestComplete method so it's hard to say what it returns.

     

    This is why I suggested FindAllChildren.  What happens if you change your code to

     

    function RelVis(){
    var page = browser.CH;
    //this return an array of all the object found with the following criteria 
    var buttonRel = page.FindAllChildren("className", "btn acute-btn rel", 100);
    
    //Log.Message(buttonRel.length);
    
    if (buttonRel.length > 0) then {
    
        if(buttonRel[0].Exists){
          buttonRel[0].Click();
        }
    }
      }

5 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Well, I don't think you gave us the correct code.  The error message is where an "Exists" property is being checked against an object that does not exist.  That is not being used in your code at all so some additional information is needed.

     

    However, probably a better way is, instead of using the native method off of contentDocument, try using "FindAllChildren" from the page level itself.  You can still search by class name and it will still return an array.

     

    Is there some reason why you're not simply mapping the button?  Find methods tend to run slower than using the NameMapping engine.

    • Al2's avatar
      Al2
      Occasional Contributor

      tristaanogre  I'm sorry, thanks for pointing that out. I made a correction in the code posted above. I was using Exists property and it failed.

       

      Thank you

      Al2

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        What's the contents of that one item in the array?  While it's true that the array may have a single element, it could be that it's a null value or simply an empty element.  The GetElementsByClassName is not a TestComplete method so it's hard to say what it returns.

         

        This is why I suggested FindAllChildren.  What happens if you change your code to

         

        function RelVis(){
        var page = browser.CH;
        //this return an array of all the object found with the following criteria 
        var buttonRel = page.FindAllChildren("className", "btn acute-btn rel", 100);
        
        //Log.Message(buttonRel.length);
        
        if (buttonRel.length > 0) then {
        
            if(buttonRel[0].Exists){
              buttonRel[0].Click();
            }
        }
          }