Extract the value from the Frame Element [C#]

In this blog, we are going to learn how to extract the value of the element which is present inside a FRAME. A FRAMESET is a collection of FRAMES. The FRAME helps to host multiple documents in a single document. But when we are doing the automation using a web browser, we can not get the element reference directly if the elements are present inside the FRAME. First, we need to get the reference of the FRAME element then we can reach the elements inside the FRAME.  

Let see how to achieve this in three different ways.

Sample HTML:

extract.html

<html>
<frameset  cols=”30%,*,30%”>

    <frame  id=’frm1′  src=”frame_1.html”>

    <frame  id=’frm2′  src=”frame_2.html”>
    <frame  src=”frame_3.html”>
</frameset>
</html>

The above HTML has three FRAME inside the FRAMESET. The following are each of the FRAME contents

frame_1.html

<html>
Content of Frame 1
<br />
<input id =”txt” type=”text”  value=”frame 1 text box”  />
</html>

frame_2.html

<html>
Content of Frame 2
<br />
<input  id =”txt” type =”text”  value=”frame 2 text box”   />
</html>

frame_3.html

<html>
Content of Frame 3
<br />
<input  id =”txt” type =”text”  value=”frame 3 text box”   />
</html>

First, navigate to the HTML page in the page load event. The navigate function will load the document which is present in the current location. In the following code, it will load the extract.html in the web browser.

private void DemoForm_Load( object sender, EventArgs e)
{
    WbBrowser.Navigate(@”D:\BlogDemo\extract.html”);
}

First method using LINQ

private void Button1_Click( object sender, EventArgs e)
{
    // Get frame using frame ID
    HtmlWindow frameWindow = (from HtmlWindow win  in WbBrowser.Document.Window.Frames select  win)
.Where(x => string.Compare(x.WindowFrameElement.Id,  “frm1”) == 0)
.FirstOrDefault();
 
    // Get first frame textbox with ID
    HtmlElement txtElement = (from HtmlElement element 
in frameWindow.Document.GetElementsByTagName( “input”)
                                             select element)
 .Where(x => string.Compare(x.Id,  “txt”) == 0).FirstOrDefault();
 
    // Check txtElement is null or not
    if(txtElement !=  null)
    {
        Label1.Text = txtElement.GetAttribute(“value” );
    }    
}

The above C# code is the first button click code. We can get the 1st FRAME element reference using LINQ query with where condition. The where condition checks the ID of the FRAME. Then the FirstOrDefault() method will return either the first sequence of an element or null if the sequence contains no element. The second step we are going to get the textbox which is present inside the FRAME element using the LINQ query. Then check the textbox reference element is null or not. If it is not null, GetAttribute() method retrieve the value of the text box and displays the value in Label1.

Second method using foreach loop

private void Button2_Click( object sender, EventArgs e)
{
    // Get frame using frame ID
    HtmlWindow frameWindow = null;
    foreach (HtmlWindow win  in WbBrowser.Document.Window.Frames)
    {
        if ( string.Compare(win.WindowFrameElement.Id, “frm2”) == 0)
        {
            frameWindow = win;
        }
    }
 
    // Get first frame textbox with ID
    HtmlElement txtElement = null;
    foreach (HtmlElement element  in frameWindow.Document.GetElementsByTagName(“input”))
    {
        if ( string.Compare(element.Id, “txt”) == 0)
        {
            txtElement = element;
        }
    }    
 
    // Check txtElement is null or not
    if (txtElement !=  null)
    {
        Label2.Text = txtElement.GetAttribute(“value” );
    }
}

In this method, in the second button click get the frame element using foreach loop iteration. Then the same way it gets the textbox element and assigns the value to the label

Third method using array

private void Button3_Click( object sender, EventArgs e)
{
    // Get list of frame
List<HtmlWindow> frameList = (from HtmlWindow win 
in WbBrowser.Document.Window.Frames select  win).ToList();
 
    // Get first frame textbox with ID
HtmlElement txtElement = (from HtmlElement element 
in frameList[2].Document.GetElementsByTagName(“input”
select element)
         .Where(x => string.Compare(x.Id,  “txt”) == 0).FirstOrDefault();
 
    // Check txtElement is null or not
    if (txtElement !=  null)
    {
        Label3.Text = txtElement.GetAttribute(“value” );
    }
}

The 3rd method illustrates how to get a frame which does not have any ID. The first step gets a list of all frame which is present inside the document. Using array index, point out the particular frame and from that, we can access the text box element.

In this article, I have explained about three ways to extract value from FRAME. If you have any questions, please leave a comment.

2 thoughts on “Extract the value from the Frame Element [C#]”

Leave a Reply

Your email address will not be published. Required fields are marked *