Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: webBrowser1 click event

Hi

I'm trying to extract links from a horrible website
Manually you have to click anywhere on the page close the popup tab if one appears then click  "<div data-type="link" class="getblock">" to display the link

How do I do this in code? I'm using a webBrowser on a Winforms

        private void FetchPageWithBtn(string URL)
        {
            webBrowser1.Navigate(URL);
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

            var Bodys = webBrowser1.Document.GetElementsByTagName("body"); // error here
            foreach (HtmlElement Body in Bodys)
            {
                Body.InvokeMember("Click");
            }



            var Divs = webBrowser1.Document.GetElementsByTagName("div");
            foreach (HtmlElement Div in Divs)
            {
                if (Div.GetAttribute("className") == "getblock")
                {
                    Div.InvokeMember("Click");
                    string InnerTxt = Div.InnerHtml;
                    LinksTextBox.AppendText(InnerTxt + Environment.NewLine);

                }
            }



        }


Open in new window


Error
System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.

Open in new window


Is there a javascript equivalent of NoFollow?
Avatar of Arana (G.P.)
Arana (G.P.)

try
HtmlElementCollection Bodys = webBrowser1.Document.GetElementsByTagName("body"); 

Open in new window

Also you maybe want to use webview2 instead of using the old webbrowser for winforms
https://docs.microsoft.com/en-us/microsoft-edge/webview2/
Avatar of trevor1940

ASKER

I tried to use webview2  it's not fully working you have to have the correct version of MS edge Canary loaded
Even there sample on Github don't work
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and then you can get

Bodys.InnerHtml
Bodys.InnerText
Bodys.OuterHtml
Bodys.OuterText

HtmlElement Class

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.htmlelement?view=netcore-3.1
Not sure the page is loading
Tried this

            webBrowser1.AllowNavigation = true;
            webBrowser1.Navigate(new Uri("http://www.google.co.uk/webhp?hl=en") );
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

            //   HtmlElementCollection Bodys = webBrowser1.Document.GetElementsByTagName("body");'
            var Bodys = webBrowser1.Document.Body;


Open in new window


body is null
you have to write that code to browser document complete event...
not button click event... I guess
Doesn't get here

       private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            MessageBox.Show("Doc loaded");

        }

Open in new window

dont use this

webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

just select the control and find the event and write a code... 
User generated image
Thought I had
I removed that line and moved the code still doesn't fire

try

webBrowser1.Url = new Uri("http://www.google.co.uk/webhp?hl=en");

Open in new window

and see if document completed fires...
Hovering over it says null
Hovering over it says null

so it loads the document, event is fired but what is null?
webBrowser1.Url = null

Open in new window



Not really sure why this isn't working

I created  a new Winforms app just to test the webBrowser control
I was able to load google so I knew I had got it working

Unfortunately the Website I need to extract the links from wouldn't load due to the embedded JavaScript
I need a better browser option