C#WebBrowser控件使用教程与技巧收集

C#WebBrowser控件使用教程与技巧收集

先来看看常用的方法

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Navigate(string urlString):浏览urlString表示的网址

Navigate(System.Uri url):浏览url表示的网址

Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息

//(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)

GoBack():后退

GoForward():前进

Refresh():刷新

Stop():停止

GoHome():浏览主页

WebBrowser控件的常用属性:

Document:获取当前正在浏览的文档

DocumentTitle:获取当前正在浏览的网页标题

StatusText:获取当前状态栏的文本

Url:获取当前正在浏览的网址的Uri

ReadyState:获取浏览的状态

WebBrowser控件的常用事件:

DocumentTitleChanged,

CanGoBackChanged,

CanGoForwardChanged,

DocumentTitleChanged,

ProgressChanged,

ProgressChanged

DocumentCompleted 页面加载完成之后的事件

复制代码


1、获取非input控件的值:

[C#] 纯文本查看 复制代码

?

01

02

03

webBrowser1.Document.All["控件ID"].InnerText;

或webBrowser1.Document.GetElementById("控件ID").InnerText;

或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");


2.获取input控件的值:

[C#] 纯文本查看 复制代码

?

01

02

webBrowser1.Document.All["控件ID"].GetAttribute("value");;

或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");


3、给输入框赋值:

[C#] 纯文本查看 复制代码

?

01

02

03

04

//输入框

user.InnerText = "myname";

password.InnerText = "123456";

webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");


4、下拉、复选、多选:

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

//下拉框:

secret.SetAttribute("value", "question1"); 

//复选框

rememberme.SetAttribute("Checked", "True");

//多选框

cookietime.SetAttribute("checked", "checked");


5、根据已知有ID的元素操作没有ID的元素:

[C#] 纯文本查看 复制代码

?

01

HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;


根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
6、获取Div或其他元素的样式:

[C#] 纯文本查看 复制代码

?

01

webBrowser1.Document.GetElementById("addDiv").Style;


7、直接执行页面中的脚本函数,带动态参数或不带参数都行

[C#] 纯文本查看 复制代码

?

01

02

03

04

Object[] objArray = new Object[1];

objArray[0] = (Object)this.labFlightNumber.Text;

webBrowser1.Document.InvokeScript("ticketbook", objArray);

webBrowser1.Document.InvokeScript("return false");


8、自动点击、自动提交:

[C#] 纯文本查看 复制代码

?

01

02

HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;

btnAdd.InvokeMember("Click");


9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

this.timer1.Enabled = true;

this.timer1.Interval = 1000 * 2;

private void timer1_Tick(object sender, EventArgs e)

{

    this.timer1.Enabled = false;

    ClickBtn.InvokeMember("Click");//执行按扭操作

}


10、屏蔽脚本错误:

[C#] 纯文本查看 复制代码

?

01

将WebBrowser控件ScriptErrorsSuppressed设置为True即可


11、自动点击弹出提示框:

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)

{

  //自动点击弹出确认或弹出提示

  IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;

  vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认

  vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示

}


12.WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

    //自动点击弹出确认或弹出提示

    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;

    vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认

    vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示

    //下面是你的执行操作代码

}


13、获取网页中的Iframe,并设置Iframe的src

[C#] 纯文本查看 复制代码

?

01

02

03

04

HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;

HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;

docFrame.All["mainFrame"].SetAttribute("src", "http://www.sufeinet.com/");


网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
14、让控件聚焦

[C#] 纯文本查看 复制代码

?

01

02

03

this.webBrowser1.Select();

this.webBrowser1.Focus();

doc.All["TPL_password_1"].Focus();


15、打开本地网页文件

[C#] 纯文本查看 复制代码

?

01

webBrowser1.Navigate(Application.StartupPath + @"\Test.html");


16、获取元素、表单

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

//根据Name获取元素

public HtmlElement GetElement_Name(WebBrowser wb,string Name)

{

    HtmlElement e = wb.Document.All[Name];

    return e;

}

 

//根据Id获取元素

public HtmlElement GetElement_Id(WebBrowser wb, string id)

{

    HtmlElement e = wb.Document.GetElementById(id);

    return e;

}

 

//根据Index获取元素

public HtmlElement GetElement_Index(WebBrowser wb,int index)

{

    HtmlElement e = wb.Document.All[index];

    return e;

}

 

//获取form表单名name,返回表单

public HtmlElement GetElement_Form(WebBrowser wb,string form_name)

{

    HtmlElement e = wb.Document.Forms[form_name];

    return e;

}

 

 

//设置元素value属性的值

public void Write_value(HtmlElement e,string value)

{

    e.SetAttribute("value", value);

}

 

//执行元素的方法,如:click,submit(需Form表单名)等

public void Btn_click(HtmlElement e,string s)

{

 

    e.InvokeMember(s);

}


17。获取Cookie

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

      static extern bool InternetGetCookieEx(string pchUrl, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);

      private static string GetCookieString(string url)

      {

          uint datasize = 1024;

          StringBuilder cookieData = new StringBuilder((int)datasize);

          if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))

          {

              if (datasize < 0)

                  return null;

              cookieData = new StringBuilder((int)datasize);

              if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))

                  return null;

          }

          return cookieData.ToString();

      }

      private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)

      {

          richTextBox1.Text = string.Empty;

          if (cbcookie.Checked)

          {

              if (checkBox1.Checked)

              {

                  richTextBox1.Text = GetCookieString(textBox1.Text.Trim());

              }

              else

              {

                  richTextBox1.Text = webBrowser1.Document.Cookie;

              }

          }

      }


18.怎么设置代理

C#webBrowser使用代理服务器的方法winform
   其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了
但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧。
这个其实是调用一个Dll文件进行设置的,
下面大家跟我一起来看看吧
首先还是要先建一个结构就是代理信息的结构体
如下

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

/// <summary>

   /// 代理结构体

   /// </summary>

   public struct Struct_INTERNET_PROXY_INFO

   {

       public int dwAccessType;

       public IntPtr proxy;//IP以及端口号

       public IntPtr proxyBypass;

   };


下面是如何 设置代理 的具体实现

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

/// <summary>

        /// 设置代理的Api

        /// </summary>

        /// <returns></returns>

        [DllImport("wininet.dll", SetLastError = true)]

        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

 

        /// <summary>

        /// 代理IP以及端口号

        /// </summary>

        /// <param name="strProxy"></param>

        private void RefreshIESettings(string strProxy)

        {

            const int INTERNET_OPTION_PROXY = 38;

            const int INTERNET_OPEN_TYPE_PROXY = 3;

 

            Struct_INTERNET_PROXY_INFO struct_IPI;

 

            // Filling in structure

            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

            struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);

            struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");

 

            // Allocating memory

            IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));

 

            // Converting structure to IntPtr

            Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

 

            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));

        }[


使用的时候也非常的简单

[C#] 纯文本查看 复制代码

?

01

02

RefreshIESettings("41.129.53.227:80");

                webBrowser1.Navigate("http://www.sufeinet.com");


这样就可以了。
19.怎么在加载完成某个页面之后执行代码

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

//本事件是当每次加载完成当前页面后才会执行的

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            //e.Url是当前加载的页面,

            if (e.Url.ToString().Contains("http://sufeinet.com"))

            {

                //执行操作1

            }

            else if (e.Url.ToString().Contains("http://baidu.com"))

            {

                //执行操作2

            }

        }


20.怎么禁止在新窗口中打开网页

[C#] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

{

        string url = ((System.Windows.Forms.WebBrowser)sender).StatusText;

        webBrowser1.Navigate(url);

        e.Cancel = true;

     

}


21.怎么设置Cookie

[C#] 纯文本查看 复制代码

?

01

webBrowser1.Document.Cookie=“你的Cookie值”;

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值