0、常用方法

 
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
 

1、获取非input控件的值:

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

2、获取input控件的值:

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

3、给输入框赋值:

//输入框
user.InnerText = "myname";
password.InnerText = "123456";
webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");

4、下拉、复选、多选:

 
//下拉框:
secret.SetAttribute("value", "question1");
//复选框
rememberme.SetAttribute("Checked", "True");
//多选框
cookietime.SetAttribute("checked", "checked");
 

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

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

根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。

6、获取Div或其他元素的样式:

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

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

Object[] objArray = new Object[1];
objArray[0] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript("ticketbook", objArray);
webBrowser1.Document.InvokeScript("return false");

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

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

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

 
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、屏蔽脚本错误:

将WebBrowser控件ScriptErrorsSuppressed设置为True即可

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

 
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");//弹出提示
}
 

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

 
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");//弹出提示
//下面是你的执行操作代码
}
 

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

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

HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
docFrame.All["mainFrame"].SetAttribute("src", "http://www.baidu.com/");

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

14、让控件聚焦

this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All["TPL_password_1"].Focus();

15、打开本地网页文件

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

16、获取元素、表单

 
//根据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);
}

webbrowser 常用方法(C#)的更多相关文章

  1. C# Webbrowser 常用方法及多线程调用

    设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...

  2. delphi webbrowser 常用方法示例

    var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...

  3. WebBrowser元素定位的常用方法

    在delphi中想要使用WebBrowser控件,需要一了解一些浏览器和网站制作的知识.操作元素(增删改查).需要提前了解HTML DOM.

  4. C#中的WebBrowser控件的使用

    0.常用方法   Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...

  5. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...

  6. 洗礼灵魂,修炼python(68)--爬虫篇—番外篇之webbrowser模块

    题外话: 爬虫学到这里,我想你大部分的网站已经不再话下了对吧?有检测报文头的,我们可以伪造报文头为浏览器,有检测IP,我们可以用代理IP,有检测请求速度的,我们可以用time模块停顿一下,需要登录验证 ...

  7. Winform控件学习笔记【第四天】——WebBrowser

    常用方法 Navigate(string urlString);//浏览urlString表示的网址 Navigate(System.Uri url);//浏览url表示的网址 Navigate(st ...

  8. C# WebBrowser控件详解

     作者:827969653     0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...

  9. c#如何判断webbrowser已经加载完毕

    最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html 红框部分便是另外加载的代码. 此 ...

随机推荐

  1. Bootstrap – 1.认识

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. 常用的scrapy setting

    原文请参考    Scrapy 爬虫入门教程十三 Settings(设置), 讲的很详细 官网参考  Settings 设置 Scrapy 设置允许您自定义所有 Scrapy 组件的行为,包括核心,扩 ...

  3. 4款Github泄漏敏感信息搜索工具简单比较

    gitrob Ruby开发,支持通过postgresql数据库https://github.com/michenriksen/gitrob weakfilescan Python开发,多线程,猪猪侠开 ...

  4. bzoj 1113

    思路:对于两张高度一样的海报 i, j, 即 y[ i ] = y[ j ], 如果对于任意i < k < j 有y[ k ] > y[ i ] && y[ k ] ...

  5. 转:win32下的堆管理系统

    转:https://bbs.pediy.com/thread-224136.htm 准备刷漏洞战争中的堆溢出部分,但是对于堆的了解较少,在这里记录一下关于堆的学习记录,有错误请各位大大拍砖 参考: & ...

  6. 【转载】retrofit 2 源码解析

    retrofit 官网地址:http://square.github.io/retrofit/ retrofit GitHub地址:https://github.com/square/retrofit ...

  7. 【我要学python】函数的系统学习

    我的短期目标:python+CTF reverse 一起加油! #1,函数介绍 1,功能性 函数目的 2,隐藏性 (封装) (避免写重复代码) 例: #round为保留小数的函数 a = 3.1415 ...

  8. java浅拷贝和深拷贝(基础也是很重要的)

    对象的copy你兴许只是懵懂,或者是并没在意,来了解下吧. 对于的github基础代码https://github.com/chywx/JavaSE 最近学习c++,跟java很是相像,在慕课网学习c ...

  9. http1.0和1.1的区别

    1.HTTP 1.1支持长连接(PersistentConnection)和请求的流水线(Pipelining)处理 HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器 ...

  10. hiho1393二分图多重匹配

    题目链接:[http://hihocoder.com/problemset/problem/1393] 题意:中文题意. 题解:二分图的多重匹配.主要是建图然后跑一个最带流,再判断一下就可以了. 建图 ...