How to click a link in the VS 2005 WebBrowser control

C

cweeks78681

I am porting some VB6 code that automates navigation through a web page
by getting all the <a> tags into a collection and then calling the
Click method on the appropriate one.

How do I click a link in a web page using the WebBrowser control in VS
2005 C# Express and .NET 2.0?
 
M

Marc Gravell

I assume you mean in Code? Try element.InvokeMember("Click");

where element is the HtmlElement for the hyperlink

Marc
 
M

Marc Gravell

A more complete example follows; note that the most efficient way to do this
would probably be to identify the anchor by ID, but this is not always
possible.

Marc

using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace Test {
static class Program {
[STAThread]
static void Main() {
using (Form form = new Form()) {
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
form.Text = "WebBrowser Demo";
form.Controls.Add(browser);
// next is inline just to share browser var
form.Load += delegate
{browser.Navigate("http://msdn2.microsoft.com");};
browser.DocumentCompleted += DocumentCompleted;
form.ShowDialog();
}
}

static void DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e) {
WebBrowser browser = (WebBrowser)sender;
foreach (HtmlElement el in
browser.Document.GetElementsByTagName("A")) {
Debug.WriteLine(el.InnerText, el.Id);
if (el.InnerText == "About MSDN2") {
el.InvokeMember("Click");
break;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top