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.

How do I get a list of links in the page

hari19hari19 Members
edited August 2009 in Sahi - Open Source
The following piece of code does not work :
for (var i = 0; i < document.links.length; i++) 
{
    var a = document.links[i];
    if(a.indexOf("somesite.com")>0)
        _alert("Hey.. somesite.com ");
}

I also tried something similar to the example at http://sahi.co.in/w/for-loops
(btw the example does not work since getLinkIds() function is not defined )

If I add
_log("Num Links = " + document.links.length);

before the for loop, it reports
Num Links = 16

but does not get into the loop. The first log message in the loop is not printed.

What am I doing wrong ?

Comments

  • dpdp Members
    The issue here is with assigning DOM objects and their properties to sahi variables and for such assignments we should use the _set() API. And the script that you used, from the For Loop example, is the one that is clearly documented as "DO NOT USE". It is given in the document to show us an example of a common MISTAKE

    You will get an example that meets your requirement from the following thread

    http://sahi.co.in/forums/viewtopic.php?id=1028
  • Thanks for the pointer!

    What I want to do is to get the value of the href for each of these links.

    When running the following code
    <browser>
    function getLinks(){
        var links = window.document.links;
        var store = [];
        for (var i=0; i<links.length; i++){
            store[store.length] = links[i].href;
        }
        return store;
    }
    </browser>
    
    _set($links, getLinks());
    
    for (var $j=0; $j<$links.length; $j++){
    _log($links[$j]);
    }
    

    I get
    http://localhost:8180/myApp/list.jsp
    http://localhost:8180/myApp/index.jsp
    http://localhost:8180/myApp/list2.jsp
    http://localhost:8180/myApp/page1.jsp
    

    where as if I check the Page Source I can see
    <a href="/myApp/list.jsp" />
    <a href="/myApp/index.jsp" />
    <a href="/myApp/list2.jsp" />
    <a href="/myApp/page.jsp" />
    

    i.e. the value of the urls for the href attribute are relative in the source whereas links[$i].href gives me the absolute url.

    Is there a way for me to get the actual value for href attribute instead of the full link ?
  • narayannarayan Administrators
    In your case, you may be able to use
    store[store.length] = links[i].pathname;
    // pathname instead of href, but this will work only if your urls are absolute (ie. start with /).
    

    For relative urls, this the behaviour:

    If the url of your page is "http://localhost:8180/abcd/index.htm";

    and your link is
    <a href="link1.jsp">link1</a>
    

    link.href = "http://localhost:8180/abcd/link1.jsp";
    link.pathname = "/abcd/link1.jsp";
    link.attributes["href"].value = "link1.jsp" (in firefox)
    link.attributes["href"].value = "http://localhost:8180/abcd/link1.jsp"; (in IE)

    Regards,
    Narayan
  • thanks again!
Sign In or Register to comment.