Forum Moderators: open

Message Too Old, No Replies

simulate user clicking automatically

         

durga

11:01 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Hi, I want to simulate user clicking automatically on a href present on a webpage. I can't copy the href and load it because the href takes some parameters automatically when we click on the href. Is there anyway that I can simulate the click on the href. Any piece of code is appreciated. I used document.getelementbyid('mylink').click but its not working. If anybody know the exact code please post it.
Thanks.

durga

11:33 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



I think document.getelementbyId('mylink') might work but How can build a HTML document using a HTML parser. I might be going wrong in this. Please Help!

tedster

7:57 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Earlier thread may help:

[webmasterworld.com...]

durga

8:07 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



I have seen this thread but I am getting errors when I used that. Here is my code. Any way I appreciate your message.

<html>
<script type="text/javascript; version=1.5">
function clickedhref() {
location.href="http://www.google.com";
alert('follow link');
document.getElementById('href="http://groups-beta.google.com/grphp?hl=en&tab=wg"').click();
}
</script>
<body onload="clickedhref()">
</body>
</html>

So I have taken google.com as my webpage and I am trying to click on a object that contains href as group-beta.google.com...
If any body know how I can Do this please suggest me.
Becoz in some webpages you will have scripts rather than direct URLs.

Thanks.

steveinspain

8:38 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



The code on the earlier thread does work
<html>
<head>
</head>
<body>
<a href="http://www.google.com" id="mylink">Click Me</a>
<script language="Javascript" type="text/javascript">
document.getElementById('mylink').click()
</script>
</body>
</html>

SuperNovaCain

8:38 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Durga,
Your snippet is malformed. You have no head so the script is just hanging there. It didn't cause any problems but in either case...

You use "getElementById" but don't use the id.
I've looked at the google page in question and the id of the link you want to simulate clicking is '2a'

Now the error "Object Expected" goes away.

Now you will get "Permission Denied".

I think that you will never be capable of simulating a click across domains because if you could, I can imagine lots of nasty things that people could be doing. I'm sure it would classify as a high security risk and though I don't know for certain, I guess it can't (and should not) be done.

You can link to a page but you can't manhandle it once you've pulled it up.

SuperN¤vaCain

ADDED:
What purpose do you have, that you need to navigate to a page outside of your domain, and then you need to "click" a link on that page, that accomplishes more than just linking to the final page? (Just wondering.)

durga

2:24 am on Jun 29, 2005 (gmt 0)

10+ Year Member



Hi SuperNovaCain,
I hope you are perfect, I think you cannot try to access script of a page like i was doing. document.getelementbyid is limited to a page that you are presently on. For example this code works just fine. IF you click on text link1 on the page you are actually clicking on the href on the text (link1 with click handler).
I found this piece of code on net when I searched for examples of clicking on hrefs in javascript. Any way thank you very much for your message now I have absolutely no doubt that I cannot do this in javascript may be I need to use VC++ Iwebbrowser objects to do this. I m working on projects that will automatically clicks and crawls through webpages. You cannot copy the URLs as sometimes you will have URLs in scripted format which will give page not found.
Try this code its intersting..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
adding click method to HTMLElement with Mozilla
</title>
<script type="text/javascript; version=1.5">
try {
// create span element so that HTMLElement is accessible
document.createElement('span');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function')
this.onclick({type: 'click'});
};
}
catch (e) {
alert('click method for HTMLElement couldn\'t be added')
}
try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target? this.target : '_self');
};
}
catch (e) {
alert('click method for HTMLAnchorElement couldn\'t be added')
}
</script>
<style type="text/css">
#testingDiv { background-color: lime; }
#testingDiv a:link { color: red; }
</style>
</head>
<body>
<div id="testingDiv">
These links are for calling the click methods of the elements below:
<a href="javascript: void 0"
onclick="document.getElementById('aDiv').click();
return false;"
>
click div
</a>
¦
<a href="javascript: void 0"
onclick="document.getElementById('aButton').click();
return false;"
>
click button
</a>
¦
<a href="javascript: void 0"
onclick="document.getElementById('link0').click();
return false;"
>
click link0
</a>
¦
<a href="javascript: void 0"
onclick="document.getElementById('link1').click();
return false;"
>
click link1
</a>
</div>
<div id="aDiv"
onclick="alert(this.id + ' ' + event.type + 'ed.')"
>
clickable div
</div>

<input type="button" id="aButton" value="clickable button"
onclick="alert(this.id + ' ' + event.type + 'ed.')"
/>
<br />
<a id="link0" href="http://www.kibo.com/">
link0 without onclick handler
</a>
<br />
<a id="link1" href="http://www.kibo.com/" target="_blank"
onclick="return confirm('Follow link?');"
>
link1 with onclick handler
</a>
</body>
</html>

Thanks everybody anyway.
Durga.