获取非input控件的值
webBrowser1.Document.All["控件ID"].InnerText;
或webBrowser1.Document.GetElementById("控件ID").InnerText; 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
获取input控件的值
webBrowser1.Document.All["控件ID"].GetAttribute("value");; 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
给输入框赋值
user.InnerText = "myname"; password.InnerText = "123456"; webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");
下拉、复选、多选
//下拉框: secret.SetAttribute("value", "question1"); //复选框 rememberme.SetAttribute("Checked", "True"); //多选框 cookietime.SetAttribute("checked", "checked");
根据已知有ID的元素操作没有ID的元素
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
获取Div或其他元素的样式
webBrowser1.Document.GetElementById("addDiv").Style;
直接执行页面中的脚本函数,带动态参数或不带参数都行
Object[] objArray = new Object[1]; objArray[0] = (Object)this.labFlightNumber.Text; webBrowser1.Document.InvokeScript("ticketbook", objArray); webBrowser1.Document.InvokeScript("return false");
自动点击、自动提交
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild; btnAdd.InvokeMember("Click");
自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助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");//执行按扭操作 }
屏蔽脚本错误
将WebBrowser控件ScriptErrorsSuppressed设置为True即可
自动点击弹出提示框
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");//弹出提示 //下面是你的执行操作代码 }
获取网页中的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.sufeinet.com/");
网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
让控件聚焦
this.webBrowser1.Select(); this.webBrowser1.Focus(); doc.All["TPL_password_1"].Focus();
打开本地网页文件
webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
获取元素、表单
//根据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); }