cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to click on an object when using GetElementsByClassName

SOLVED
Occasional Contributor

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. 

 

image.png

 

Thank you

Al2

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Unable to click on an object when using GetElementsByClassName

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();
    }
}
  }

Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

5 REPLIES 5
Highlighted
Community Hero

Re: Unable to click on an object when using GetElementsByClassName

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.


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Occasional Contributor

Re: Unable to click on an object when using GetElementsByClassName

@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

 

Highlighted
Community Hero

Re: Unable to click on an object when using GetElementsByClassName

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();
    }
}
  }

Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

Highlighted
Community Manager

Re: Unable to click on an object when using GetElementsByClassName

Thank you tristaanogre!

 

@Al2 did you have a chance to try what tristaanogre is suggesting? 


Sonya Mihaljova
Community and Education Specialist

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!
Highlighted
Occasional Contributor

Re: Unable to click on an object when using GetElementsByClassName

@tristaanogre  Thanks much! That helps.