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、給輸入框賦值:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("value", "控件值");
4、CheckBox選中:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("Checked", "true");
5、觸發select事件
select賦值:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("selected", "selected");
webBrowser1.Document.GetElementById("控件ID").SetAttribute("value", "控件值");
觸發事件:
1)webBrowser1.Document.GetElementById("控件ID").SetAttribute("selectedIndex", “選項序號”);
2)HtmlElement selScript = doc.CreateElement("script");
selScript.SetAttribute("type", "text/javascript");
/注釋/// ajax//
///selScript.SetAttribute("text", "function _seloption(){$.ajax({url:'http://ncp.nifdc.org.cn/AjaxHandler/GetFoodTypeThird.ashx',type:'get',data:{parent_id:'948'},success:function(data){if(data!=null){$('#slt_food_type_third').empty();$('#slt_food_type_third').append(data);},complete:function(){}});}");
/注釋 ajax/
selScript.SetAttribute("text", "function _seloption(){$('#" + idname + "').trigger('change')}");
HtmlElement head = doc.Body.AppendChild(selScript);
doc.InvokeScript("_seloption");
6、根據已知有ID的元素操作沒有ID的元素:
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
根據Parent,FirstChild,Children[1]數組,多少層級的元素都能找到。
7、獲取Div或其他元素的樣式:
webBrowser1.Document.GetElementById("addDiv").Style;
8、直接執行頁面中的腳本函數,帶動態參數或不帶參數都行:
Object[] objArray = new Object[1];
objArray[0] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript("ticketbook", objArray);
webBrowser1.Document.InvokeScript("return false");
9、自動點擊、自動提交:
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
btnAdd.InvokeMember("Click");
10、自動賦值,然后點擊提交按鈕的時候如果出現腳本錯誤或一直加載的問題,一般都是點擊事件執行過快,這時需要借助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");//執行按扭操作
}
11、屏蔽腳本錯誤:
將WebBrowser控件ScriptErrorsSuppressed設置為True即可
12、自動點擊彈出提示框:
參考:http://www.cnblogs.com/qqflying/archive/2012/07/25/2608038.html
13、獲取網頁中的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/");
14、網頁中存在Iframe的時候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一樣,前者是主框架的Url,后者是當前活動框口的Url。
15、讓控件聚焦
this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All["TPL_password_1"].Focus();
16、獲取class
// he 是HtmlElement對象
// GetAttribute("class") 一直取空值
he.GetAttribute("className")
轉載自:https://blog.csdn.net/bluecard2008/article/details/84312612?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase
