hi,
i want to programmatically click a link on a asp.net web site. first i ve navigated to that website and i ve got a code from some other website

For i As Integer = 0 To Me.WebBrowser1.Document.Links.Count - 1
If Me.WebBrowser1.Document.Links(i).InnerHtml.StartsWith("Learn HTML") Then
Me.WebBrowser1.Document.Links(i).InvokeMember("Click")
End If
Next

but i dont know how and where to use it in my program code since im new to asp.net. can anybody help.

Thanks in advance.
Expecting fast replies guys.

Dani AI

Generated

Brief summary and recommended approaches (addresses , ties back to and )

The VB/WebBrowser snippet shown in the thread is for a WinForms/browser-control scenario (as already pointed out) and does not belong in server-side ASP.NET. Two practical approaches exist for automating a login + click + download workflow:

  • Server-side HTTP automation (best when the site exposes a direct download URL after authentication).
  • Browser automation (best when the site relies on JavaScript/AJAX, client-side events, or download dialogs).

Server-side (recommended when possible)

  1. Emulate the browser: GET the login page, parse out any hidden tokens (VIEWSTATE, anti-CSRF), POST credentials while preserving cookies, then GET the final file URL and save the response bytes. HtmlAgilityPack (suggested earlier by @kvprajapati) is useful to extract hidden inputs and links. Minimal C# pattern:
var handler = new HttpClientHandler { CookieContainer = new CookieContainer(), AllowAutoRedirect = true };
using var client = new HttpClient(handler) { BaseAddress = new Uri("https://target.site") };
var loginHtml = await client.GetStringAsync("/login");
var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(loginHtml);
// extract tokens here...
var post = new FormUrlEncodedContent(new[] { new KeyValuePair<string,string>("username","u"), new KeyValuePair<string,string>("password","p"), /* tokens */ });
await client.PostAsync("/login", post);
var fileBytes = await client.GetByteArrayAsync("/path/to/file");
File.WriteAllBytes(@"C:\temp\file.dat", fileBytes);

Browser automation (when JS/AJAX or dialogs are required)
Selenium WebDriver or Playwright can drive a real browser, perform clicks, and auto-save downloads by setting browser download preferences. Example pattern with Selenium + Chrome:

var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", @"C:\downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
using var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://target.site/login");
driver.FindElement(By.Id("user")).SendKeys("u");
driver.FindElement(By.Id("pass")).SendKeys("p");
driver.FindElement(By.Id("loginBtn")).Click();
driver.FindElement(By.CssSelector("img.download")).Click();

Troubleshooting and cautions
Use the browser DevTools Network tab to capture exact requests, headers, and form names (critical for correct POST data). If the site uses AJAX, dynamically-generated tokens, or CAPTCHA, server-side requests may fail and a headless/real browser will be required. Preserve cookies and Referer headers when needed. Respect the site’s terms of service and protect stored credentials. For scheduled runs, run the chosen automation (headless browser or background service) on a machine with the required drivers and a controlled download path.

Recommended Answers

All 14 Replies

>click on a asp.net webpage link programmatically

Use HttpWebRequest and it's methods.

>click on a asp.net webpage link programmatically

Use HttpWebRequest and it's methods.

thanks for ur reply,
since im new to .net i dnt know how to use this HttpWebRequest and its method in our progrm. So it will be helpful if u explain here with some sample code and how to use it in our program.

thanks..

Hi,

The code that you posted is to be used in WebBrowser control in Windows Forms application. It is not intend to be used in asp.net application.

In what do you want to click link? Server side or cliend side?

If you want to click a link using JavaScript, use the following code.

function ClickLink() { 
  document.getElementById('SomeLinkId').click(); 
}

thaks for ur reply Ramesh S,
but i ve tried as u said it does not take me to the corresponding page. for example i ve used bing site. this is t he code i ve used.

<html>
<head>
<script language="javascript">
function ClickLink() { 
document.webservices.submit();  
document.getElementById('sb_feedback').click(); 
}
</script>
</head>
<body>
<a href="javascript:ClickLink()">click me</a>
<form name="webservices" method=post action="http://www.bing.com/" target="new_window" onSubmit="window.open('', 'new_window', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes)"> 
</form>
</body>
</html>

check where im wrong.. expecting ur reply soon.

thanks

hey i really wondered that no body ve the knowledge in this stream to help me! y cant u help me regarding this since i need this for my project..


come on guys .... make ur reply soon..

>click on a asp.net webpage link programmatically

You can use Response.Redirect("uri") method.

thanks for replying,

but i cant achieve it using response.redirecct(url). i want to click an image button on some others website from my application..

pls do help..

expecting ur reply..

Please be more specific. Why don't you tell us what you're trying to do and we can tell you how to go about it.

hi,

every day i want to download some files from a clients web site and its my work.. so im trying now to automate that process to simplify my work if i did this it may help for others too in my office. my working process manually is
1.login into the website
2.click on a image button there
3.which will lead to next page of that site and there will be link to be clicked
4. when we click that link it will display the dialog box to open save or cancel the downloading attachments as usual.

i need to automate this..

i think im clear now..

help me to do this.

You can't click on that button/image programatically but you can get that resource using WebRequest if that button/image expose a url.

You can use HtmlAgilityPack (utility) to read/parse the html page and prepares the collection of links of resources.

Experts,
i ve asked a simple question only i think.. but i feel it difficult since im very new.. dont know why any of u not showing interest towards this.. and why u r not ready to share ur knowledge here and help people like me?

thanks for ur interest ,

And r u sure it can not be done programmatically... but i need to automate this process. so can u give some more details about how to use and where to implement this HtmlAgilityPack ..

thanks.

>i ve asked a simple question only i think

Yes it is but you failed to explain.

>dont know why any of u not showing interest towards this.

Undoubtedly no. Read our posts. You failed to show your interest since you haven't tried to put your work here.

>so can u give some more details about how to use and where to implement this HtmlAgilityPack

I don't seems any problem with googling.

Take a look :

1.
2. http://blogs.msdn.com/smourier/archive/2003/06/04/8265.aspx
3. http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack
4. http://stackoverflow.com/questions/655603/html-agility-pack-parsing-tables

Thanks for replying..

ill seen through it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.