2

So, I'm creating a bot using webBrowser in c# that loads a website entered in the text box. When the website is loaded, I need bot to click on a specific anchor text. After that when a new page is loaded, I need to click on another anchor text and so on, until a form to fill out details appears. I also need to show captcha to the user where he/she can fill it and submit it, so that the page can continue to next page.

What I need is to invoke different methods, each time the browser navigated to next page and loading is complete. I have successfully created a WebBrowser_DocumentCompleted, but it get invoked over and over again, due to the fact that same hyper link is present on the page that I want to visit. But, on that page I need to click on a button.

I did this for getting the link and visiting it.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Select the html element by inner text of anchor and click on it
        HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("a");
        foreach (HtmlElement el in elc)                
        {
            if (el.InnerText == null || el.InnerText.Equals("Matching text"))
            {
                el.InvokeMember("click");
            }
        }

    }

After this the link that have matched innretext get clicked and the page loads. The page have same anchor text and it gets loaded again and again. But, I need to click on another button and go to next page.

So, if you have any way that I can use to do it then it would be awesome.Any help is welcomed!

P.S. I'm a beginner in C# and .net

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Ayush
  • 33
  • 4
  • Check [this](http://stackoverflow.com/a/19718530/1768303) and [this](http://stackoverflow.com/a/19063643/1768303). – noseratio Aug 26 '15 at 22:24

1 Answers1

1

The behaviour you see is normal, I suppose the page you are loading has some iframes or embedded content and for each one loaded the DocumentCompleted will be fired (it's not related on having a link to the page, a link does nothing until it's clicked).

You must take actions based on the Url parameter of the WebBrowserDocumentCompletedEventArgs passed on thos function, in this way you can execute the required action for each concrete page, something like this:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

    switch(e.Url.ToString())
    {
        case "http://myfakeserver.com/mypageone.htm":

            //Do whetever you want to do
            break;

        case "http://myfakeserver.com/mypagetwo.htm":

           //Do more stuff
           break;
    }

}

Hope it helps.

EDIT:

Ok, now I get what you need.

It's easy, just check if you area already on that page.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Select the html element by inner text of anchor and click on it
    HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("a");
    foreach (HtmlElement el in elc)                
    {
        var hRef = el.GetAttribute("href");

        if(string.IsNullOrWhitespace(hRef))
          continue;

        var lnkUri = new Uri(hRef);

        //If the link points to this page, ignore it
        if(lnkUri.Segments[lnkUri.Segments.Length - 1] == e.Url.Segments[e.Url.Segments.Length - 1])
            continue;

        if ((el.InnerText == null || el.InnerText.Equals("Matching text"))
        {
            el.InvokeMember("click");
        }
    }

}

Beware in the example i'm just checking the last part of the url, so if you have different paths which have the same page name it will fail, you must adapt it to your needs, depending on how the uris are written on the href you can do a full check of the urls.

Gusman
  • 14,905
  • 2
  • 34
  • 50
  • So, by doing this I can call it multiple times on each page that satisfy any of the cases I define? BTW how do I define cases with Url, since the user input custom urls? – Ayush Aug 26 '15 at 17:20
  • sorry but I don't understand, ¿the user can input any URL he wants? Maybe if you explain a bit what you're really trying to do I can explain it better. – Gusman Aug 26 '15 at 17:25
  • Well, I need to create a shopping add to cart bot for a specific site. The site add a page for each sale. The url are different all the time. The page displays several images of product with product name as hyperlink. Click on that and you'll get to the product page. The Url however changes to site.com/product/product-name. When clicked purchase the url changes to site.com/cart. Where two button appear and I need to click on checkout. In checkout page there is a form that need to be filled also automatically. Let me know if I need to give more info. – Ayush Aug 26 '15 at 17:30
  • Okokoko, now I get it, let me modify my answer – Gusman Aug 26 '15 at 17:31
  • Its looping again on the same page... Do I need to change anything, except the matching text – Ayush Aug 26 '15 at 18:25
  • Add a breakpoint and check the hRef and Url, maybe the server is masking the real addresses – Gusman Aug 26 '15 at 19:22
  • Not working mate!! Also URL update are shown in the text box, as I'm updating it – Ayush Aug 26 '15 at 19:43
  • But are the URL and the hRef the same or not? that's for what I asked to put a breakpoint and check them – Gusman Aug 27 '15 at 00:19