HI thanks for free info
i would really like some help with c#

i want some c# code which will check web browser control for
a link which has at the start example: http://clickmefirst.com/random bull
so it will check page untill it finds a link with http://clickmefirst
then it will click the link _self

really gratefull for your help

i found this code

foreach (HtmlElement link in webBrowser1.Document.GetElementsByTagName("a"))
{
    if (link.InnerText.Equals("Google Me"))
    {

    }
}

Recommended Answers

All 12 Replies

Does it really need to "click" the link or just navigate to the address?

Does it really need to "click" the link or just navigate to the address?

when a user goes onto a page and clicks a link that is the effect i want to acheive
so all the correct refereals etc.. get given

...but what you're after is the effect of what's on the next page, right?
You can simulate tjat by just opening the next page.
If the second page really needs a referral (from the previous page), you can do that with the HttpWebRequest/Response construction.

It really depends on what you're after, though.

...but what you're after is the effect of what's on the next page, right?
You can simulate tjat by just opening the next page.
If the second page really needs a referral (from the previous page), you can do that with the HttpWebRequest/Response construction.

It really depends on what you're after, though.

ok so webbrowser loads page www.blahblah.com

on page there is link http://sdlfhsd;fjhsdfjhs;jfhs;kjdfhsdlfhsdkjfhsdfj

i want a way for the code to sarch page for link each link has a common name

and just click it as though you or me clicked it


thanks for your time buddy

I understand that part.
What is going to happen when it is "clicked"?

Do you:
1) just want to SEE the next page in a browser
or
2) want SOMETHING from that next page?

the next page will just show in same page

so i want the code to click link and it will show in same page _self

and thats all ;)

thanks again

In this example, the user will need to enter a partial URL.
If found in the WebBrowser control, it will navigate to that link.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DW_417357_CS_FORM
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         List<string> lst_strLinks = new List<string>
         {
            "one <a href='http://www.textpad.com'>TextPad</a>",
            "two <a href='http://www.DaniWeb.com'>DaniWeb</a>",
            "three <a href='http://www.Bing.com'>Bing</a>",
         };

         StringBuilder sbLinks = new StringBuilder(lst_strLinks.Count);
         lst_strLinks.ForEach(s => sbLinks.Append(s+"<br />"));
         webBrowser1.DocumentText = sbLinks.ToString();
      }

      private void bnNavigate_Click(object sender, EventArgs e)
      {
         if (string.IsNullOrEmpty(txtUrlFragment.Text))
         {
            MessageBox.Show("Please enter a partial URL");
            return;
         }

         // A very simplistic regex for capturing links
         Regex rxLinks = new Regex(@"'(?<url>http://.*)'");

         string strLink = // could be null or empty
            // split the contents of the text/html in the web browser
            webBrowser1.DocumentText.Split("<>\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            // check to see if they are links
            .Where(s => rxLinks.IsMatch(s)).Select(s => rxLinks.Match(s).Groups["url"].Value)
            // check to see if they match what the user entered and get the first one.
            .Where(s => s.ToLower().Contains(txtUrlFragment.Text.ToLower())).FirstOrDefault();

         if (string.IsNullOrEmpty(strLink))
         {
            MessageBox.Show("Not found");
            return;
         }

         webBrowser1.Navigate(strLink);
      }
   }
}

Here is a better form-load :)

private void Form1_Load(object sender, EventArgs e)
      {
         webBrowser1.DocumentText = string.Join("<br />", new string[]
         {
            "one <a href='http://www.textpad.com'>TextPad</a>",
            "two <a href='http://www.DaniWeb.com'>DaniWeb</a>",
            "three <a href='http://www.Bing.com'>Bing</a>"
         });
      }

hi there , let me say first thank you very much for your amazing effort

i tried to compile this code and i have 2 errors

Error 1 The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\users\randy\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Form1.cs 3 14 WindowsFormsApplication5
Error 2 'System.Array' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) c:\users\randy\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Form1.cs 46 17 WindowsFormsApplication5


cheers

You should be compiling for .NET 3.5 or higher and you might need to add System.Core to the references section in the project.

You should be compiling for .NET 3.5 or higher and you might need to add System.Core to the references section in the project.

hi there but i am useing .net 2.0

and that is what i need
for easier compatibility for users

OK. You can take out the LINQ and code the loops manually.

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.