Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, windows and java based applications. Get your 30 day free trial.

Discuss your Sahi Pro usage patterns, best practices, problems and solutions. Help others solve their problems and seek help from the community when needed. If you need specific support on your application, please email support @

Opening folder tree with getElementsBy...

edited May 2013 in Sahi - Open Source
Can any one point me to an example for the common test of returning a collection or array by "getElementsByName"?

i do this regularly with VBS and QTP, but am trying to convert those scripts to Sahi.

For instance: I want to click on each of 200-500 closed nodes in different folder "trees" marked by <div class=abc id=img01n4><img src="plus.gif" title="xyz">definition of xyz</div>

Recording this just gives me 200-500 lines with every ID - useless unless I can use some glob or regex pattern of ID to return an array? I thought surely this has already been documented - but haven't seen any of the "getelement" functions in the help examples?


  • edited June 2013
    Well, I was 99% wrong, sort of. I found the answer, deep inside the help, after finding an EXAMPLE in the demo folder of "_collect".

    I have posted the code below for the next person to find, with less friction loss!

    (I call the function 2-3 times, one for each of two nested levels.
    A clone of the function handles a single node identified by DIV rather than SPAN.
    I failed to open the topmost of each inner/second level node until I ran the loop "backwards" from top to bottom.
    And I absolutely failed with no workaround (so far) to open the top most "zero-th" node, even though its id = "img157span1"
    openNodes(); // first pass
    openNodes();	// second / inner set  of nodes
    openDivNodes(); // one lonely stubborn node
    openNodes();	  // the last hold outs (the zero-th) inner / second level nodes
    openLastNode();  // a failed but harmless attempt to open the top most outer mode
    function openNodes() {
    var $plusgifs = _collect ("_image", "/plus.gif/", _in(_image("image")));  // 
    _set($Count, $plusgifs.length);
    	for (var $i = $Count - 1; $i > 0 ; $i--) 
    function openDivNodes() {
    var $plusgifs = _collect ("_image", "/plus.gif/", _in(_div("stuff1div1")));  // 
    _set($Count, $plusgifs.length);
    for (var $k = 0; $k < $Count - 1; $k++)	{ 
    function openLastNode() {
    var $somegifs = _collect ("_image", "/plus.gif/", _in(_span("cellPic"))); 
    _set($Count, $somegifs.length);
    //	for (var $m = 0; $m < $Count - 1; $m++)  // didn't help either way
    	for (var $m = $Count - 1; $m > 0 ; $m--) 
    pasted revealHTML here for reference
    [<div id="stuff1div1">]<span id="stuff14imgspan" ><img class="image" id="stuff14imag1"  src="plus.gif" ...>...stuff...</span>[</div>]
    alternative to getElementByID getElementByName getElementByClass getElementByClassName
  • Thanks for sharing and congratz on discovering the _collect API ;)
Sign In or Register to comment.