18 January 2026:


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.

Count of links in a page

edited November -1 in Sahi - Open Source
Hi Guys,

I am using the below code to count no of links in a page
and i am using the if condition to compare the link text and
if it matches i want to execute the code in the if condition
but the its not working can anyone please help me in this

var $anchors = [];
function getLinkIds()
{
var retVal = [];
var links = document.links;
for (var a=0; a<links.length; a++)
{
retVal [a]= links[a].innerHTML;
}
return retVal;
}

_set($anchors, getLinkIds());

for (var $a=0; $a<$anchors.length; $a++)
{
if (_condition(( "Claims Status" == _getText(_link(a)))))
{
_click(_link("Claims Status"));
}
}

Comments

  • Hi prasanth

    I will look at this problem more closly tomorrow, but just by reading the code now, I would say that you have to declare arrays by

    var $sampleArray= new Array();

    I don't know if this is really needed, but thats how I remember working with Arrays.
    Also I m not sure if _set($anchors, getLinkIds()); works as you expect, i would write a

    _set($anchors[a], links[a].innerHtml);

    into the getLinkIds() function. Please excuse me when I'm telling bullshit, I can't test it right now

    Does the Sahi logs say anything about what went wrong?
  • I can see the below error in the logs

    _sahi.setServerVar('\$anchors', getLinkIds());
    TypeError: Object expected
    No trace available
  • This example isn't working with the controller


    var $arr1= new Array();
    var $arr2= new Array();

    function set(){
       for (var $i=0; $i<10; $i++ ) {
          $arr1[$i] =  $i*4;
       }
       return $arr1
    }
    _set($arr2, set());

    ////// create 2 testarrays, fill one up with some numbers, try the _set() API to copy the values to $arr2
    Error: _sahi.setServerVar('\$arr2[\$i]', set());
    ReferenceError: set is not defined

    While this is working fine:

    var $arr2= new Array();
    var $arr1= new Array();
    function set(){
       for (var $i=0; $i<10; $i++ ) {
          $arr1[$i] =  $i*4;
    _set($arr2[$i], $arr1[$i])
       }
    }
    set();

    so please try to change your code as I mentioned the post before.

    Regards
    Wormi
  • Thanks globalworming for ur support
  • narayannarayan Administrators
    Hi Prashant,

    You seem to want to click on all links with text "Claims Status".

    You could use this:
    <browser>
    function getLinkIds()
    { 
        var retVal = []; 
        var links = document.links; 
        for (var a=0; a<links.length; a++)
        { 
            retVal[a]= _getText(links[a]); 
        } 
        return retVal; 
    }
    </browser>
    
    var $anchors = []; 
    _set($anchors, getLinkIds());
    var $counter = 0;
    for (var $a=0; $a<$anchors.length; $a++) 
    {
        if ("Claims Status" == $anchors[$a])
        {
             _click(_link("Claims Status[" + $counter + "]"));
            $counter++;
        }
    }
    

    You could also do the same with:
    for (var $i=0; $i<100; $i++) 
    {
        var $el = _link("Claims Status[" + $i + "]");
        if (_exists($el))
        {
             _click($el);
        }
    }
    

    Hope that helps.

    Regards,
    Narayan
  • Thanks a lot for your support....
Sign In or Register to comment.