Common usage examples of C using webbrowser
- 2021-07-24 11:42:26
- OfStack
This article illustrates the common usage of C # using webbrowser. Share it for your reference. The details are as follows:
Judge whether the network is normal or not
private bool IsConnectedToInternet()
{
int Desc;
return internet.InternetGetConnectedState(out Desc, 0);
}
Set the default page:
webBrowser1.Navigate(new Uri("http://www.yoursiteweb.com/copy.aspx"));
Login code:
private void btnlogin_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("id").InnerText =txtuser.Text.ToString().Trim();//fill name
webBrowser1.Document.GetElementById("passwd").InnerText =txtpwd.Text.ToString().Trim();//fill pwd
HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];
formLogin.InvokeMember("submit");
Thread.Sleep(5000);
herfclick("http://www.yoursiteweb.com/upload.aspx");// This is a successful login operation , Jump to //upload.asp
// Note that you don't jump directly , Simulated click link
//SESSION Will not be lost
herfclick("http://www.yoursiteweb.com/copy.aspx");
}
Simulated click link:
private void herfclick(string url)
{
for (int i = 0; i < webBrowser1.Document.All.Count; i++)
{
if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() ==url)
{
webBrowser1.Document.All[i].InvokeMember("click");// Trigger " CLICK "Events
break;
}
}
}
Run the JS function in the Web page
private void callJsMethod(string jsfun,string jsParameter)
// Function name , Function parameter
{
HtmlDocument doc = webBrowser1.Document;
doc.InvokeScript(jsfun,new object[]{jsParameter});
}
Fill in data:
private void filldate(string txt)
{
HtmlElement frmWord = webBrowser1.Document.Forms["frmWord"];// Find from
HtmlElementCollection txtarea = frmWord.GetElementsByTagName("TEXTAREA");
HtmlElementCollection radio = frmWord.GetElementsByTagName("INPUT");
radio[2].InvokeMember("click");// Select radio Control
txtarea[0].InnerText = txt.ToString();
}
I hope this article is helpful to everyone's C # programming.