Skip to content

Instantly share code, notes, and snippets.

@sphingu
Last active October 12, 2022 13:04
Show Gist options
  • Save sphingu/5781036 to your computer and use it in GitHub Desktop.
Save sphingu/5781036 to your computer and use it in GitHub Desktop.
Using WebBrowser in WPF
----------------------------------------------------
// Using WebBrowser for Crowling in WPF
----------------------------------------------------
<WebBrowser Cursor="Arrow" Name="MyBrowser" LoadCompleted="MyBrowser_OnLoadCompleted" />
----------------------------------------------------
// useful methods of WebBrowser
----------------------------------------------------
MyBrowser.Navigate(new Uri("http://google.com"));
private void MyBrowser_OnLoadCompleted(object sender,NavigationEventArgs e)
{
...
// return source of loaded page in WebBrowser
var document=(IHTMLDocument3) MyBrowser.Document;
//Get Element and set its value
document.getElementById("userName").setAttribute("value","myusername");
//get Button on page and fire its click event
document.getElementById("btnSubmit").click();
// Invoke javascript function on page loaded on WebBrowser
MyBrowser.InvokeScript("submitform",param1,param2,...);
//get data from table in page
_innerHtmldata =
((IHTMLDocument3) MyBrowser.Document).getElementById("datatable")
.innerHTML;
}
@saidaniwael
Copy link

i get error on IHTMLDocument3
could not be found
????? solution???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment