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
Error
Is there a javascript equivalent of NoFollow?
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);
}
}
}
Error
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Is there a javascript equivalent of NoFollow?
Also you maybe want to use webview2 instead of using the old webbrowser for winforms
https://docs.microsoft.com/en-us/microsoft-edge/webview2/
https://docs.microsoft.com/en-us/microsoft-edge/webview2/
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
Even there sample on Github don't work
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and then you can get
Bodys.InnerHtml
Bodys.InnerText
Bodys.OuterHtml
Bodys.OuterText
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.1ASKER
Not sure the page is loading
Tried this
body is null
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;
body is null
you have to write that code to browser document complete event...
not button click event... I guess
not button click event... I guess
ASKER
Doesn't get here
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("Doc loaded");
}
dont use this
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
just select the control and find the event and write a code...
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
just select the control and find the event and write a code...
ASKER
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");
and see if document completed fires...
ASKER
Hovering over it says null
Hovering over it says null
so it loads the document, event is fired but what is null?
ASKER
webBrowser1.Url = null
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
Open in new window