Back on 3/9/2005, I published a blog titled
Website Testing - Automation, Autofill, etc. (C# WinForms).
Since then, dozens of people have downloaded it and used it successfully to build projects with similar technology.
Today, "Herman" asked how to populate forms, click buttons, etc. in frames. Of course, frames are a big no no, but they do exist.
This blog explains how to do what Herman is asking for.
Download code
The only real difference from the previous blog is lines 5-29. In lines 5-14, I iterate through the frames collection and find
the specific frame by name. on line 20, I load that frame into its own IHTMLDocument2 object. And finally, on lines 22-28, I find
the controls and manipulate them. That's it!
1//Get the web browser document
2myDoc = new HTMLDocumentClass();
3myDoc = (HTMLDocument) WebBrowser.Document;
4
5//Find the frame named "content"
6FramesCollection myFramesColl = myDoc.frames;
7IHTMLWindow2 myContentFrame = null;
8for (int i = 0; i < myFramesColl.length; i++)
9{
10object refIndex = i;
11 mshtml.IHTMLWindow2 frame = (mshtml.IHTMLWindow2)myFramesColl.item(ref refIndex);
12if (frame.name == "content")
13 myContentFrame = frame;
14}
15
16//Frame found?
17if (myContentFrame != null)
18{
19//Load into IHTMLDocument2 object
20 IHTMLDocument2 myContentFrameDoc = myContentFrame.document;
21
22//Find the textbox
23 HTMLInputElement objTextBox = (HTMLInputElement)
myContentFrameDoc.all.item("txtEmail", 0);
24 objTextBox.value = txtEmail.Text;
25
26//Click the selected button
27 HTMLInputElement btnSearch = (HTMLInputElement)
myContentFrameDoc.all.item("cmdJoin", 0);
28 btnSearch.click();
29}
Comments
|
On
1/8/2010
Angel
said:
Hello, when i start your program and load web site with iframes there some error is ocure- "Error:Access Denied ...".
On
1/8/2010
Matt
said:
Hi,
On
8/10/2006
Ron Burgandy
said:
That doesn't make sense
On
8/10/2006
Brian Fantana
said:
60% of the time it works everytime
|
Leave a Comment