18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
trouble getting getElementsByClassName to work in sahi
Hi
I am using the following function to return an array of elements associated with a certain class.
I call it like this but it returns a length of 0 instead of 8 as expected. It is basically looking for all TR elements that have a class='data'
Thanks
Tom
I am using the following function to return an array of elements associated with a certain class.
/*Written by Stuart Colville from muffinresearch co uk*/
function getElementsByClassName($strClass, $strTag, $objContElm) {
$strTag = $strTag || "*";
$objContElm = $objContElm || document;
var $objColl = $objContElm.getElementsByTagName($strTag);
if (!$objColl.length && $strTag == "*" && $objContElm.all){
$objColl = $objContElm.all;
}
var $arr = [];
var $delim = $strClass.indexOf('|') != -1 ? '|' : ' ';
var $arrClass = $strClass.split($delim);
for (var $i = 0; $i < $objColl.length; $i++) {
var $arrObjClass = $objColl[$i].className.split(' ');
if ($delim == ' ' && $arrClass.length > $arrObjClass.length){
continue;
}
var $c = 0;
$comparisonLoop:
for (var $k = 0; $k < $arrObjClass.length; $k++) {
for (var $m = 0; $m < $arrClass.length; $m++) {
if ($arrClass[$m] == $arrObjClass[$k]){
$c++;
}
if (( $delim == '|' && $c == 1) || ($delim == ' ' && $c == $arrClass.length)) {
$arr.push($objColl[$i]);
break $comparisonLoop;
}
}
}
}
return $arr;
}
I know it works correct because I have tested it outside of Sahi.I call it like this but it returns a length of 0 instead of 8 as expected. It is basically looking for all TR elements that have a class='data'
var $tr_class = getElementsByClassName('data', 'tr');
var $len= $tr_class.length;
How do I get this to work in Sahi, I always thought that Sahi was just Javascript? But even this does not work as expected$ted =document.getElementsByTagName('tr');
$tedLen = $ted.length;
Any pointers greatly appreciated as I have been messing around with this problem all day to no avail!!Thanks
Tom
Comments
Could you also show how you are using this in your script.
And if possible could you also post the parsed script?
Most probably you are not using it inside a _set function.
try this: and
I have created a slimmed down version as a demo and here is the parsed version of that demo script Within it you can see the 3 different ways I tried to call the function. Each one is separated with something like _debug('000000000000000'). I have them commented out so only one runs at a time.
Here is the html code that the test is run against If you need anything else just let me know.
Thanks
Tom
Cheers
Tom
Sahi script is javascript, but is parsed and mangled a bit.
Keep these in mind when using variables:
1) Sahi does not care about variables being prefixed with $, unless you use it inside a scheduler function. In your code, you could get rid of all $s in getElementsByClassName function.
2) Use _set only when a variable's value is initialized based on the state of a particular page, and cannot be determined at the writing of the script.
Eg. For a loop of 10, you could just do But to get the length of the rows in a table on the 3rd screen and then iterate over them, you would have to do, 3) When assigning a value to a $ variable, (i.e. a variable which is going to be used in a scheduler function), assign only constant values, which will survive a JSON serialization and a round trip to the proxy. That means that DOM elements are not to be assigned to Sahi variables. You could however store an array of ids, or array of indexes.
So _set($trs, getElementsByClassName("data", "tr")) is wrong.
But _set($trIds, getIdsArray(getElementsByClassName("data", "tr"))) would be fine.
_set($en, getElementsByClassName("data", "tr").length) is also fine.
4) As far as possible, find the dom element using a function invoked from the call to the scheduler function.
In your case, add a function getSingleElement(class, tag, counter) which can be invoked from say, the _log() function.
Eg. _log(_getText(getSingleElement("data", "tr", $i)), 'custom2');
So your script could be changed like this to make it work:
I am glad you asked this question, because it gave me a reason to clarify my thoughts around this and state it as rules.
It was great to be spoon-fed the solution in understandable bites
Cheers
Tom
This function doesn't work fine in Internet Explorer 6. Only in Firefox it works well. In Internet Explorer doesn't Sahi fill the queue, if this function is used before my testscript is attached, but the script with this function is really parsed by Sahi Controller.
Do you know, why can't be used in Internet Explorer 6?
Greetz
Greetz