Is it possible to automatically click on a link in a webbrowser?
Printable View
Is it possible to automatically click on a link in a webbrowser?
yes you can...
Refer
can you explain how to do it? Or give me some sample code...
This code will click the Link as Highlighted (http://blogs.msdn.com/bsinghal/)
Code:Private Sub Form_Load()
WebBrowser1.Navigate "www.msdn.com"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
WebBrowser1.Document.All("ctl00_mainContentContainer_ctl01_ctl08").Click
End Sub
In you example you have
but that does not give you the page that you made an image of. I tried your example and I got a completely different page than the one you show in the attached picture. You have to use http://msdn.microsoft.com/hi-in/default.aspx to get that page so in your code example why did you use www.msdn.com (which will not give you the same page) when you should have used the second URLCode:Private Sub Form_Load()
WebBrowser1.Navigate "www.msdn.com"
End Sub
Am I missing something here?
What is '"ctl00_mainContentContainer_ctl01_ctl08"?
a link or a button for a link uses this
Code:Private Sub Command1_Click()
Dim HTML As HTMLDocument
Dim href As HTMLAnchorElement
Set HTML = WebBrowser1.Document
For Each href In HTML.links
If href.innerText = "Settings" Then ' change Settings to the anchor text of the link
href.Click
End If
Next
End Sub
It is the id of a link on the page. You need to navigate to the URL I stated in my previous post to find it. Do not navigate to www.msdn.com but the other one: http://msdn.microsoft.com/hi-in/default.aspx. On that page you will see the link like danasegarane posted in his picture. Below is the link as it appears in the HTML document pageQuote:
Originally Posted by garylazz
So in your code if you do this:Code:<a id="ctl00_mainContentContainer_ctl01_ctl08" onclick="javascript:Track('ctl00_mainContentContainer_ctl01_ctl06|ctl00_mainContentContainer_ctl01_ctl08',this);" href="http://blogs.msdn.com/msdnindia"><strong>The India Developer Team Blog</strong>
</a>
it will be the same as if you manually clicked on that link on the page which would take you to the onclick="javascript:...... part and then onto the URL listed afterwards which is http://blogs.msdn.com/msdnindiaCode:WebBrowser1.Document.All("ctl00_mainContentContainer_ctl01_ctl08").Click
This is one way to do what you asked in your original post.
newprogram showed you in post #7 how to set up a loop to acompolish this. So for this particular case your code could be like this (based on what newprogram posted)
Before you can do this however make sure that the page has completely loaded. The _DocumentComplete Event is the event to let you know the document has fully loaded.Code:Private Sub Command1_Click()
Dim HTML As HTMLDocument
Dim href As HTMLAnchorElement
Set HTML = WebBrowser1.Document
For Each href In HTML.links
If href.innerText = "ctl00_mainContentContainer_ctl01_ctl08" Then
href.Click
Exit For
End If
Next
End Sub
Can you explain to me how to list all the links on a web page and then how i can click on one i want?
Well, here is one way to do it. Of course you will need to modify it for your own purpose. Put a WebBrowser control on the Form. Also put a Listbox and a Command button on the Form.
Code:Dim MyURL As String
'
' You need to add a Reference to Microsoft HTML Objects Library
'
Private Sub Command1_Click()
MyURL = "http://www.vbforums.com/forumdisplay.php?f=1&page=1&order=desc"
WebBrowser1.Navigate MyURL
Caption = MyURL
End Sub
Private Sub LoadLinks()
Dim HTML As HTMLDocument
Dim href As HTMLAnchorElement
Set HTML = WebBrowser1.Document
List1.Clear
'
' Go through the HTML source code and look for every element that is a hyperlink
' and extract it out and put it in a listbox
'
For Each href In HTML.links
'
' Add each hyperlink to the listbox. A hyperlink is <A ..........>Visible Text You See</A>
'
List1.AddItem href.outerHTML
Next
End Sub
Private Sub List1_Click()
Dim s As String
'
' s will be the hyperlink you clicked on from the listbox
'
s = List1.List(List1.ListIndex)
Dim HTML As HTMLDocument
Dim href As HTMLAnchorElement
Set HTML = WebBrowser1.Document
'
' Search through the HTML source code and find the same link that was
' selected from the listbox
'
For Each href In HTML.links
If InStr(href.outerHTML, s) > 0 Then
'
' The selected link was found in the HTML so now we click on it
'
Caption = s
href.Click
Exit For
End If
Next
LoadLinks
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If InStr(1, URL, MyURL) <> 0 Then
LoadLinks
End If
End Sub
OK, works quite well... except it doesnt load all the links. It wont display the advertisment links. How can i display these add links?
Give an example of a link that it wont load or display. Also, post the URL of the page that has these links that you are talking about. That's the only way I can test them.
advertisment links? this is not some bot your trying to make to click on ppc ads is it?
Quote:
Originally Posted by garylazz
No it isnt a ppc bot. I am wanting to make an app that will browse to a src page, load all the links on that page and then goto each of those links and save those pages as well.
This is an example of a link with an add on it: http://searchportal.ucoz.com/index.htm
In post #11 you stated...
...and in post #12 I responded with...Quote:
Originally Posted by garylazz
...and then you responded to that in post #14....Quote:
Originally Posted by jmsrickland
I ran the URL you posted above and here is the HTML source code from that URLQuote:
Originally Posted by garylazz
Note that in the source code thare are five hyperlinks that I highlighted in red and in the snapshot of the program I gave you it shows five links in the listbox display below the browser window.Code:<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Space Game MMORPG 1.03</title>
</head>
<body bgcolor="#000000" text="#008080">
<p>Hello all.</p>
<p>This is a new project of mine and will be updated whenever I can.</p>
<p>This is a MMORPG (Massive multiplayer online role playing game. On this page
you can find a link to the <a href="http://www.canning.co.nz/Space%20Game%20Server%201.03.zip">server</a>
and the <a href="http://www.canning.co.nz/Space%20Game%20Client%201.03.zip">client</a>.
Both are free.</p>
<p>Screenshot:</p>
<p><img border="0" src="../SpaceGame1.JPG" align="center" width="915" height="709"></p>
<p>Server link: <a href="http://www.canning.co.nz/Space%20Game%20Server%201.03.zip">Space%20Game%20Server%201.03.zip</a></p>
<p>Client link: <a href="http://www.canning.co.nz/Space%20Game%20Client%201.03.zip">Space%20Game%20Client%201.03.zip</a></p>
Your browser does not support JavaScript. Update it for a better user experience.
Powered by ExoClick.com <a href="http://www.exoclick.com">Pay Per Click Program</a>.</noscript>
<!-- END ExoClick.com Ad Code -->
</body>
<!-- BEGIN ExoClick.com Ad Code -->
<script type="text/javascript" src="http://syndication.exoclick.com/ads.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color=000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only="></script>
<noscript>
</html>
Now, please tell me exactly which advertisment link(s) from your URL it wont load or display because I fail to see the problem.
If you load the page, and scroll to the bottom you will see an 'Exoclick' add. Your code doesnt load the links to do with this add.... is this because it is text/javascript?
Is it possible to get the refs to these adds?
Yes, that is the reason. You asked for links. That is not a link. A link is an element that begings with <a.... and ends with </a> tags.Quote:
Originally Posted by garylazz
Yes it is possible to pull these out. It just requires additional search loops. It would appear in the list box as this:
<script type="text/javascript" src="http://syndication.exoclick.com/ads.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color =000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only="></script>
Is this what you want? It will not be pulled out as the three seperate clickable links you see in the box area and I am not even sure once it is extracted out you could even click on it in the same sense as you can click on an actual hyperlink because those three ads you see are generated by the Java script and to do so you would have to execute the Java script code to generate those adds and that is a horse of another color.
Sorry to post here but...is it possible that i can submit all the text in a multiline textbox to a webpage....for example the body part of Yahoo compose are ?????
OK, I played around with that Java script that contains a URL to the Exoclick Website and I wasn't able to get anywhere with it.
You can simply load the following into your WebBrowser and see what I mean. Copy the following source code and save it as a .html file on your hard drive.
and then using your program with the WebBrowser control on it navigate to that file like this:Code:<html>
<script type="text/javascript" src="http://syndication.exoclick.com/ads.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color=000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only="></script>
</html>
and you will see the box with the three adds in it in your WebBrowser window but you won't be able to extract out any of the three links because they do not exist in the source code. I don't know how that works. I even tried to go directly to the website by putting the URL, http://syndication.exoclick.com/ads....ub=&text_only=, in the IE Address box but got an error and I even trie using it with the WebBrowser control like this:Code:'
'
WebBrowser1.Navigate App.Path & "\saved_file.html"
'
'
and I got the same error that I got using IE.Code:'
'
WebBrowser1.Navigate http://syndication.exoclick.com/ads.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color=000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only=
'
'
What I don't understand is that although I got errors going to that website I see that you can click on the URL link in the above paragraph and it will open up that website and allow you to download the ads.php file but using that same URL with IE and WebBrowser generated errors. Don't know, strange stuff.
Although I wasn't able to get it to work by placing the URL in IE I did find out that if you change it from the Java script code to an actual link you will get the download dialog box. Like this:
Now if you download the file and look at it's contents you will see the following code:Code:<html>
<a href="http://syndication.exoclick.com/ads.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color=000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only=">CLICK HERE</a>
</html>
Note that the code is writing out a document as an iframe. You could now probably use that code to generate the iframe for the box with the three add links and then by using iframe parameters in your VB program possibly extract out each of the three links you want. Now I don't have the time to mess around with this so if this is that important to you then you need to get help from someone else who has the time to mess with this.Code:var dt=new Date().getTime();
document.write('<div style="width: 260px; height: 340px; text-align: left;"><iframe style="border: 1px solid #000000;" frameborder="0" scrolling="no" width="260" height="340" src="http://syndication.exoclick.com/ads-iframe-display.php?type=260x340-thumbs&login=searchportal&cat=311&search=&ad_title_color=000000&bgcolor=FFFFFF&border=1&border_color=000000&font=&block_keywords=&ad_text_color=000000&adult=0&sub=&text_only=&p=&dt=' + dt + '"></iframe>');
document.write('</div>');
So basically it cannot be done because it loads an iframe... is this correct?
I didn't say it cannot be done. Read the last paragraph of my previous post. I said .....Note that the code is writing out a document as an iframe. You could now probably use that code to generate the iframe for the box with the three add links and then by using iframe parameters in your VB program possibly extract out each of the three links you want..Quote:
Originally Posted by garylazz
Also please not that it doesn't load an iframe; it loads a java script element. That means when the page loads into your WebBrowser you get the <script.... to </script> tags. That's what you see in the source code of the HTML document. During run-time IE (and here I am not sure how it is done) takes the in-between string and somehow downloads the ads.php file from the server. Then inserts the file into the document page. I showed you what the file contents looks like. Just think in your head that the actual contents of the file is in the document and not the link to it. Now IE executes the code within the script tags which in turn creates an iframe that contains the data that displays the box with the three links in it. But you don't see this in the original document source because this is done during run-time and not load time. So, the bottom line is that the VB program has no access to these links. This is what I think is going on. Now it may be possible as I mentioned above that if you knew how to get down to the iframe data you might be able to extract out the ad links in that box but that is a complicated thing to do and like I said I dont have the time to mess with it. Besides, this is only for this particular page and other documents might have a completely different approach to display ad links so the VB code to work with this sort of situation would be quite complex. Now if you want to pursue this issue I suggest that you start a new thread and ask for help in this matter but it is beyond my ability to do this. I do know how to get to an iframe in a particular example but that is only if the iframe was already embedded in the source code but in this case the iframe is created on the fly so here I do not know how to handle it.
If you want to experiment around with this here are some quidelines.
Load the page into your WebBrowser control
Once the page has fully loaded extract out the java script
Change the java script to a hyperlink element (see my post #19)
Execute the hyperlink. This will invoke the download of the ads.php file
Get the file and remove the document.write statements so that only the actual HTML is left
Save this as a .html file
Using WebBrowser navigate to that file you just saved. Now you have the actula iframe embedded within your source code
Using iframe parameters in your VB code extract out the three hyperlinks that are in the box.
You think this is just a little complex? Well it is. And that is why I can't mess with it. It's too much trouble to go through just for the sake of these three ad links. And then what about the next time you get a document page and the whole process is different from this one. Now what do you do?
I don't know, gary, maybe someone else around here might have a little more insight into this matter and be able to help you but it's just a little too complicated for me
thank you anyway.... i really appreciate your help :)