How to manipulate webpage elements in WebBrowser2012-12-20 14:21 188 people read Comments (0) favorite reports
1. Gets the value of the non-input control:
webbrowser1.document.all["Control ID"]. InnerText;
or WebBrowser1.Document.GetElementById ("Control ID"). InnerText;
or WebBrowser1.Document.GetElementById ("Control ID"). GetAttribute ("value");
2. Gets the value of the input control:
webbrowser1.document.all["Control ID"]. GetAttribute ("value");;
or WebBrowser1.Document.GetElementById ("Control ID"). GetAttribute ("value");
3. Assign values to the input box:
WebBrowser1.Document.GetElementById ("Control ID"). SetAttribute ("Value", "Control Value");
4. CheckBox selected:
WebBrowser1.Document.GetElementById ("Control ID"). SetAttribute ("Checked", "true");
5. Manipulate elements without IDs based on elements known to have an ID:
HtmlElement btndelete = webBrowser1.Document.GetElementById (Passengerid). PARENT.PARENT.PARENT.PARENT.FIRSTCHILD.FIRSTCHILD.CHILDREN[1]. Firstchild.firstchild;
Depending on the parent,firstchild,children[1] array, the number of levels of elements can be found.
6. Get the style of div or other elements:
WebBrowser1.Document.GetElementById ("Adddiv"). Style;
7, directly execute the script function in the page, with dynamic parameters or without parameters are the line:
object[] Objarray = new Object[1];
Objarray[0] = (Object) this.labFlightNumber.Text;
WebBrowser1.Document.InvokeScript ("Ticketbook", Objarray);
WebBrowser1.Document.InvokeScript ("return false");
8, Automatic Click, automatic submission:
HtmlElement Btnadd = doc. getElementById ("Adddiv"). FirstChild;
Btnadd.invokemember ("click");
9, automatic assignment, and then click the Submit button when the script error or the problem is always loaded, the Click event is usually executed too fast, you need to delay the execution of the Submit button event with the help of the Timer control:
This.timer1.Enabled = true;
This.timer1.Interval = 1000 * 2;
private void Timer1_Tick (object sender, EventArgs e)
{
this.timer1.Enabled = false;
Clickbtn.invokemember ("click");//Perform a push-button operation
}
10. Masking script Error:
Set the WebBrowser control scripterrorssuppressed to True
11, automatically click the pop-up prompt box:
Reference: http://www.cnblogs.com/qqflying/archive/2012/07/25/2608038.html
12. Get the IFRAME in the Web page and set the src of the IFRAME
HTMLDocument docframe = webbrowser1.document.window.frames["MainFrame"]. Document;
Or
HTMLDocument docframe = webbrowser1.document.all.frames["MainFrame"]. Document;
docframe.all["MainFrame"]. SetAttribute ("src", "http://www.baidu.com/");
13, the existence of the IFRAME in the Web page Webbrowser1.url and webbrowser1_documentcompleted in the E.url is not the same, the former is the main frame URL, the latter is the current active box port URL.
14. Let the control focus
This.webBrowser1.Select ();
This.webBrowser1.Focus ();
Doc. all["Tpl_password_1"]. Focus ();
Note: Original works reproduced please indicate the source, other users have other aspects about WebBrowser control has other operation skills can message exchange, thank you!
How to manipulate webpage elements in WebBrowser