This is a migrated thread and some comments may be shown as answers.

How to Extract the InnerText content from the div tag and write it in an excel document

7 Answers 1387 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sivaramasubramanian
Top achievements
Rank 1
Sivaramasubramanian asked on 02 May 2014, 03:16 PM
How to Extract the InnerText content from the div tag and write it in an excel document?

I have a html response as given below,

<div class= "my class" id= "myid" style = "mystyle">
   Account has been successfully created. Your Account number is 12345


Now from the above div tag i want to extract innertext content "12345" and store it in an external excel file. Please guide me in this.

7 Answers, 1 is accepted

Sort by
0
Sivaramasubramanian
Top achievements
Rank 1
answered on 05 May 2014, 06:03 PM
So far i have tried the below methods to extract the innertext content from the div tag and display it in the LOG file,

1. HTMLFindExpression exttext = new HTMLFindExpression("id=~myid","class=~myclass","innertext=^Account has been successfully");
   Log.WriteLine (exttext);

2. Element exttext1 = Find.ByContent("p:Account has been successfully submitted.");
   Log.WriteLine (exttext1.ToString());

3. Element exttext2 = Find.ByContent("p:<div id='myid'>", FindContentType.InnerText);
   Log.WriteLine (exttext2.ToString());

4. Element exttext21 = Find.ByContent("p:<div class='myclass' id='myid' style='mystyle'>", FindContentType.InnerText);
   Log.WriteLine (exttext21.ToString());

5. IList<Element> exttext3 = Find.AllByContent("p:Account has been successfully submitted.");
   Log.WriteLine (exttext3.ToString());
 
From the above 5 methods 1,3,4 and 5 doesn't work and the 2nd method displays the incorrect text as "Script21".
0
Madhu
Top achievements
Rank 1
answered on 06 May 2014, 03:34 PM
Hi Siva,

I was in a same page and wrote below script it works fine. My case is I'm storing two values into spreadsheet (Portfolio Name and Security name). First you need to extract result into a variable.

Public Sub Extracting_Portfolio_Security_DataSource()
'Start Script Created on 8-9-2011 for storing extracted data into excel spreadsheet.
Dim MyPortfolioSecurityExcel As Object
Dim MyPortfolioSecurityBook As Object
Dim MyPortfolioSecuritySheet As Object
Dim MyPortfolioSearch As String
Dim MySecuritySearch As String

'Start a new workbook in Excel.
MyPortfolioSecurityExcel = CreateObject("Excel.Application")
MyPortfolioSecurityBook = MyPortfolioSecurityExcel.Workbooks.Add
MyPortfolioSecuritySheet = MyPortfolioSecurityBook.Worksheets(1)

'Printing PortfolioName log
Dim ExtPortfolioName As String = Me.GetExtractedValue("PortfolioName")
'Dim ExtPortfolioName As String = "Portfolio"
Log.WriteLine("'Portfolio' Extracted to Excel Datasource: '"+ ExtPortfolioName +"'")

'Exporting PortfolioName String into Excel Sheet
MyPortfolioSearch = "PortfolioName"
MyPortfolioSecuritySheet.Range("A1").Value = MyPortfolioSearch
MyPortfolioSecuritySheet.Range("A2").Value = ExtPortfolioName

'Printing SecurityName log
Dim ExtSecurityName As String = Me.GetExtractedValue("SecurityName")
'Dim ExtSecurityName As String = "Security"
Log.WriteLine("'Security' Extracted to Excel Datasource: '"+ ExtSecurityName +"'")

'Exporting SecurityName String into Excel Sheet
MySecuritySearch = "SecurityName"
MyPortfolioSecuritySheet.Range("B1").Value = MySecuritySearch
MyPortfolioSecuritySheet.Range("B2").Value = ExtSecurityName

'Save the Workbook.
MyPortfolioSecurityBook = Nothing
MyPortfolioSecuritySheet.SaveAs("C:\TrunkBuildScripts\Data\ExtractedPortfolioSecurity.xlsx")
MyPortfolioSecurityBook = Nothing

'Quit Excel
MyPortfolioSecurityExcel.Quit()
MyPortfolioSecurityExcel = Nothing
MyPortfolioSecuritySheet = Nothing
MyPortfolioSecurityBook = Nothing
GC.Collect()
'End Script Created on 8-9-2011 for storing extracted data into excel spreadsheet.
End Sub

Let me know how it goes?

Thanks,
Madhu
0
Boyan Boev
Telerik team
answered on 07 May 2014, 08:12 AM
Hello,

@Madhu, thank you for helping others, we really appreciate it. I have added you Telerik points.

@Siva, if you need additional assistance, please let us know.

Thank you!

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Sivaramasubramanian
Top achievements
Rank 1
answered on 07 May 2014, 02:27 PM
Hi,

@Madhu & Boyan Boev, Thanks a lot for your Reply, I will try it in my Scenario and Update you about the Status. Can you assist me in extracting the Innertext content in the div tag as given in my 1st post.

--
Thanks,
Siva
0
Boyan Boev
Telerik team
answered on 09 May 2014, 08:17 AM
Hi Siva,

Here is the code which writes the InnerText into the log:

HtmlDiv myDiv = ActiveBrowser.Find.ById<HtmlDiv>("myid");
Log.WriteLine(myDiv.InnerText);

Let me know if this helps. 

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Sivaramasubramanian
Top achievements
Rank 1
answered on 09 May 2014, 06:32 PM
Hi Boyan Beov,

Thanks for you reply, I have used the code given by you and i am facing an exception as "InnerException:System.NullReferenceException:
Object reference not set to an instance of an object.". Is there anything needs to be added along with your code?

Thanks,
Siva.
0
Boyan Boev
Telerik team
answered on 12 May 2014, 05:07 AM
Hi Siva,

That means that the find expression is incorrect (i.e. the id is wrong or something else).

Please double check the ID and your find expression.

I have created this expression against the example that you gave me.

Hope this helps.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Sivaramasubramanian
Top achievements
Rank 1
Answers by
Sivaramasubramanian
Top achievements
Rank 1
Madhu
Top achievements
Rank 1
Boyan Boev
Telerik team
Share this question
or