Hey.
I have a webbrowser control in my C# program which is loading a site that is build up by frames.
So when I use webbrowser.DocumentText I only get the source of the main site where all the frames are loaded.
Futher more there are input fields in the main frame where I want to set innertext and use Invoke member at. But instead of focus on the frame, the webbrowser focus on the document where all the frames are loaded, so I get an error when I am trying to set Innertext and so on.
Is there any way that I can focus on the frames so I can set innertext in input fields, and get the source of the different frames that are loaded ?
Best Regards
Mads
Read HTML and manage frames in webbrowser control
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mads3 pointsPosted Feb 23, 2009, 2:01 PM EST
webBrowser1.Document.Window.Frames[x]
But haven't find any way to make it work.
Does anyone got an idea?
Neal Schafer2 pointsPosted Mar 18, 2009, 12:16 PM EDT
An example of usage, assuming you have a webbrowser control named wb1 and are wanting to set an input field on a page where the input field has an id of "Last_Name", is as follows:
HtmlElement el = getElement(wb1.Document, "Last_Name","id");
if (el != null)
el.SetAttribute("value","yourlastname");
The function will return null if the element is not found. The type parameter can be either "id" or "name" referring to if we are looking for the id or name tags of the element. If there are multiple elements with the same name and you are searching by name this will only return the last value. The code could be easily modified to change that.
Hope this helps,
Here's the Function:
private HtmlElement getElement(HtmlDocument doc, string id, string type)
{
HtmlElement el = doc.All[id];
if (type == "id")
{
el = doc.GetElementById(id);
if (el == null)
{
if (doc.Window.Frames.Count > 0)
{
for (int i = 0; i < doc.Window.Frames.Count; i++)
{
HtmlDocument tmpDoc = doc.Window.Frames[i].Document;
if (tmpDoc.GetElementById(id) != null)
{
el = tmpDoc.GetElementById(id);
}
}
}
}
}
if (type == "name")
{
el = doc.All[id];
if (el == null)
{
if (doc.Window.Frames.Count > 0)
{
for (int i = 0; i < doc.Window.Frames.Count; i++)
{
HtmlDocument tmpDoc = wb1.Document.Window.Frames[i].Document;
HtmlElementCollection coll = tmpDoc.All.GetElementsByName(id);
if (coll != null && coll.Count > 0)
{
el = coll[0];
}
}
}
}
}
return el;