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.

Getting all Links of a page into a variable one by one

kaushikkaushik Members
edited November -1 in Sahi - Open Source
Hi,

Can any one one tell me how to get all the links present in a webtable.

Example : one coming to the Sahi forum page we get several posts in a web table.
Say I wanna capture all the links into a variable one by one.

Regards,
kaushik

Comments

  • Hi,

    You can get all the links in a web page into a variable like this:
    var $x = new Array();
    _set($x,  top.document.links);
    
    For extracting links specifically from a web table alone, I think more thinking would be needed :)

    Regards,
    Pankaj.
  • Thanks a lot pankaj.

    I Understand that taking values from the table requires more thinking and I will do it.

    I am trying to get the names of the links on a message box. And following is the code


    _setValue(_textbox("q"), "fhgfgh");
    _click(_submit("Google Search"));
    var $x = new Array();
    _set($x, top.document.links);
    _alert($x[1]);

    But when I run this it throws an error. I dont know why can you help me on this.
  • I used a similar alert here but it works fine for me. I am using an older version of sahi. if u r using the latest version (with rhino) you may have to use a function that will return the array of stored links. and use the tag <browser></browser> around the function. Hope it will help you.
    For further reading go to:
    http://sahi.co.in/w/scripting-changes-in-sahi-v2

    Regards,
    Pankaj.
  • Thanks Pankaj. That was really an eye opener for me. I tried by creating a function and call it in the following way.

    _setValue(_textbox("q"), "bang");
    _click(_submit("Google Search"));
    _alert(getlinkname());

    <browser>
    function getlinkname(){
    return (top.document.links[2]);
    }
    </browser>

    Now It shows me the alert with the URL of the link and not the link name.
    Have I done it the right way.

    regards,
    kaushik
  • Yup :)
    Now You can fiddle around with the idea to get links from a web table.
    All the best

    Pankaj.
  • How about this to get the link name:

    return (top.document.links[2].name);

    Were you trying to get the text encapsulated by the link tags? In that case, you would want to use the innerHTML property

    return (top.document.links[2].innerHTML);

    You can read about it here:
    http://www.w3schools.com/htmldom/prop_anchor_innerhtml.asp

    You'll get more mileage out of Sahi if you familiarize yourself with the HTML Document Object Model.
    http://www.w3schools.com/htmldom/default.asp
Sign In or Register to comment.