Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to find a link and click it (need help)?

    Hello everyone,

    First of all, I'm not a noob on javascript (I know the basics), but I need your help.
    What i need, is to make the script to find a link, and click it. In example...
    There is a page, with 10 links, everytime you load the page, the links changes (it starts like normal, but has diferent endings), so i need the script to find the link that i need, and click it.
    It would be great if you help me

    Thanx,
    Plytas

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Easiest way would be to assign IDs to your links that will remain unchanged and the script would then use getElementById("idoflink") and click on that. Hope this helps.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The problem is, that I can't change the page. I can make only script.

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    In which case it would be nice if we could see the code we have to work on. I mean, if it does assign IDs already, that can be used... or if it assigns names to the links, etc.

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    There is the page code:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
    <wml><card id="index" title="MetalScape 19:47 03-05">
    <p align="center"><small><a href="zaisti.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=use_points">Turi 26 nepanaudotus lygio taskus</a><br/>
    	&gt;&lt;&gt; * &lt;&gt;&lt;<br/></small></p>
    
    <p align="center"><small>
    <b>Tu miske, koki medi kirsi?</b><br/>
    [x kiek_kartu] (minim medkircio lygis)<br/>
    &gt;&lt;&gt; * &lt;&gt;&lt;<br/>Jusu medkirtystes lygis: <b>218435.1</b><br/></small></p>
    
    <p align="left"><small>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=alk&amp;kd=52428">Alksnio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=alk&amp;kd=52428&amp;kiek=40">x40</a>] (0)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=iev&amp;kd=52428">Ievos</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=iev&amp;kd=52428&amp;kiek=20">x20</a>] (50)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=glu&amp;kd=52428">Gluosnio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=glu&amp;kd=52428&amp;kiek=10">x10</a>] (300)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=top&amp;kd=52428">Topolio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=top&amp;kd=52428&amp;kiek=7">x7</a>] (1000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=kle&amp;kd=52428">Klevo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=kle&amp;kd=52428&amp;kiek=5">x5</a>] (3000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=azu&amp;kd=52428">Azuolo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=azu&amp;kd=52428&amp;kiek=3">x3</a>] (5000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=uos&amp;kd=52428">Uosio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=uos&amp;kd=52428&amp;kiek=3">x3</a>] (8000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=mau&amp;kd=52428">Maumedzio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=mau&amp;kd=52428&amp;kiek=3">x3</a>] (11000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=bao&amp;kd=52428">Baobabo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=bao&amp;kd=52428&amp;kiek=3">x3</a>] (15000)<br/>
    #<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=rau&amp;kd=52428">Raudonmedzio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=rau&amp;kd=52428&amp;kiek=2">x2</a>] (30000)<br/>
    
    </small></p>
    <p align="center"><small>
    &gt;&lt;&gt; * &lt;&gt;&lt;<br/><a href="meniu.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=">I pradzia</a><br/><b>Miskas:</b><br/>
    plytas<br/>
    
    </small>
    <input name="chat" type="text" maxlength="100" title="Chat" value=""/><br/>
                <anchor>Sakyk<go href="/miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=med" method="post">
            <postfield name="chat" value="$(chat)"/>
            </go></anchor><small></small></p></card></wml>
    As you can see, there are the links, every time i load this page, the link ending changes.
    And i can't change this code.

    BTW. the text is in lithuanian

  • #6
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Going through all the childnodes proved to be rather annoying, but in the end:

    Code:
    <script type="text/javascript">
        function selectlink() {
            p = document.body.childNodes;
            small = p[5].childNodes;
            links = small[0].childNodes;
            clicky = links[3].href;
            window.location=clicky;
        }
    </script>
    This essentially points the browser to the link, called x40 in the code you provided me. There probably is a better way to go about it (with a loop that runs through the entire child nodes list there and looks for a specific link that is called... "Alksnio" for an example, but... this should do the trick, provided the link you need to click has the same position every time the web page is generated) Give it a whirl, see how it works for you. Maybe someone else will give a better solution in the mean-time.

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hmm... I guess I am noob
    How can I get it to work?

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Actually, here's a much better solution:

    Code:
    <script type="text/javascript">
    	function selectlink() {
    		p = document.getElementsByTagName("a")
    		clicky = p[1].href;
    		window.location = clicky;
    	}
    </script>
    All in all, choosing which link to go to is in the line "clicky = p[1].href;". Just alter the number there to point to the specific link. This would work... but once again depends on the link being in the same position all the time. As to making it actually do what it's supposed to... I guess assign an onclick event to a button that will trigger the function like this:

    Code:
    <input type="button" onclick="selectlink()" value="test" />
    If you need more assistance, don't hesitate to ask.

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The thing is that, the page is not mine...
    I can only make js file...

  • #10
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    When do you want the function to activate? It certainly helps knowing that.

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    When the page loads...
    I could use redirect function, but link changes...

  • #12
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    In which case the code would look like:

    Code:
    <script type="text/javascript">
    	window.onload = function selectlink() {
    		p = document.getElementsByTagName("a")
    		clicky = p[1].href;
    		window.location = clicky;
    	}
    </script>
    This would force it to follow the link when the page loads. Once again, it relies on the link you want followed being in the same location all the time, and ofcourse, altering the number in "clicky = p[1].href;" will designate the specific link. Hope this helps.

  • Users who have thanked Eldarrion for this post:

    Plytas (03-06-2009)

  • #13
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I guess I won't make this to work... I create js file:
    Code:
    // ==UserScript==
    // @name           name
    // @namespace      namespace
    // @include        http://ms.miestas.biz/miskas.php?w=&nick=plytas&pass=2a6848e1cb92e0985dabc65cbcd1b52f&id=med
    // ==/UserScript==
    
    window.onload = function selectlink() {
    	p = document.getElementsByTagName("Alksnio")
    	clicky = p[2].href;
    	window.location = clicky;
    }
    But it won't work... Am I that bad?

  • #14
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    One major problem that I see in your code (marked in red):

    Code:
    // ==UserScript==
    // @name           name
    // @namespace      namespace
    // @include        http://ms.miestas.biz/miskas.php?w=&nick=plytas&pass=2a6848e1cb92e0985dabc65cbcd1b52f&id=med
    // ==/UserScript==
    
    window.onload = function selectlink() {
        p = document.getElementsByTagName("Alksnio")
        clicky = p[2].href;
        window.location = clicky;
    }
    There is a reason for the "a" tag - it points to anchor elements, whereas there is no "Alksnio" tag in HTML, so you're essentially... selecting nothing. And from that point on... the entire script would break and even get a pretty error in FireBug.

  • #15
    New Coder
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Still no efect... I load the page, but nothing hapens...


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •