Please note that Sahi's development and support teams do not actively monitor this. This is more a community forum. If you are a Sahi Pro user looking for commercial support, please email support @ with your query.

Opening folder tree with getElementsBy...

qawebqaweb Members
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?


  • qawebqaweb Members
    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.