How to iterate visible rows alone using UFT - vbscript

when i iterate this below webtable,i am getting row count as 3(with hidden row).
but i can see only 2 rows in my application.
i can get row count with help of descriptive programming,but i want to iterate only the rows which are visible.
<table>
<tbody>
<tr class="show">Name</tr>
<tr class="hide">Ticket</tr>
<tr class="show">city</tr>
</tbody>
</table>
i have tried this below code,but its displays hidden row text as well,
for i=1 to rowcount
print oWebtable.getcelldata(i,2)
next
Actual Output-
Name,
Ticket,
city
expecting output-
Name,
city

UFT has no way knowledge of your show/hide class names. If you want to filter out some rows you need to do it yourself.
Set desc = Description.Create()
desc("html tag").Value = "TR"
desc("class").Value = "show"
Set cells = oWebtable.ChildObjects(desc)
Print "Count: " & cells.Count
For i = 0 To cells.Count - 1
Print i & ": " & cells(i).GetROProperty("inner_text")
Next
Note that I had to add TD elements to your table in order for this to work since it's invalid HTML to have text in a TR element.

Related

Show the total of a column in the top of a table

I have a table and content of the column are coming from a Database. In the bottom of the table I am able to show the total value as well. But I want to show the total at the top of the table because sometimes there are hundreds of Rows, and to see the total I have to scroll to the end.
Is that even possible?
I have tried the below code. Which is giving me the total at the end of the table. But I want to show the total at the top of the table
<Table>
// This will create the column names
// I need to get the Total here above these column names
<tr>
<td> Product </td>
<td> Qty </td>
<td> Price </td>
</tr>
<% set ObjRS =ObjConn.execute ( "select * from pricetable")
total= 0
Do while Not ObjRS.EOf
product = objrs("product")
qty = objrs("Quantity")
price = ObjRS("price")
total = total+ price
response.write "<td>" & product & "</td><td>" & qty & "</td><td>" & price & "</td><td>"
objrs.movenext
loop
response.write "<tr><td> & Total & </tr></td>
</table>
You could just temporarily save the output and then output it in the order you like (I don't know the language you are using there, so there might be some slight changes to make, but in general that would be the most basic solution):
<Table>
// This will create the column names
// I need to get the Total here above these column names
<tr>
<td> Product </td>
<td> Qty </td>
<td> Price </td>
</tr>
<% set ObjRS =ObjConn.execute ( "select * from pricetable")
sum = 0
outText = ""
Do while Not ObjRS.EOf
product = objrs("product")
qty = objrs("Quantity")
price = ObjRS("price")
total = sum + price
outText = outText & "<tr><td>" & product & "</td><td>" & qty & "</td><td>" & price & "</td></tr>"
objrs.movenext
loop
response.write "<tr><td> & total & </td></tr>"
response.write outText
</table>
from
response.write "<tr><td> & total & </td></tr>"
to
response.write "<thead><tr><th> & total & </th></tr></thead>
I would do a second query that calculates the SUM().
Vbscript can have buffer issues if you try to loop through and assign it all to variables — especially for a lot of data. The thead trick is hack-ish and undermines your structure. Best to calculate the sum directly

Get data from two last columns in table

I'm trying to retrieve data from all the cells in the two last columns of a table with an unknown number of rows and columns. I have to do this by VBScript which I'm having difficulties with.
In the example below there are 2 rows with each 5 columns. However in my situation, the number of rows and columns varying.
I would like to get the data in the last two columns, being Year 2015 and Year 2016 in first row and 444 and 555 in second row etc. Since the rows and columns are varying I cannot find a fitting script for retrieving the data.
The data should be listed as variables, as I need to parse them into an input field.
<table id="calculations_data" class="key_figures togglable">
<tr>
<th></th>
<th scope="col">Year 2012</th>
<th scope="col">Year 2013</th>
<th scope="col">Year 2014</th>
<th scope="col">Year 2015</th>
<th scope="col">Year 2016</th>
<th></th>
</tr>
<tr id="turnover_data" class="data_row addaptive">
<th class="title">Turnover</th>
<td>111</td>
<td>222</td>
<td>333</td>
<td>444</td>
<td>555</td>
</tr>
</table>
Furthermore the data is located in a website which I access by this script:
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://data.biq.dk/users/sign_in"
Note: The website above requires login which I manage via my script. The login details cannot be provided.
I have had success with the code below where the data is located in a tag.
Data_CompanyName = IE.document.getElementsByTagName("h1")(0).innerText
Document.getElementByID("company_name").value = Data_CompanyName
We can extract the innerHTML of that table first and parse that innerHTML as we parse XML. Not sure if this is the best approach but it worked for me.
Try this code:
set objIE = CreateObject("internetexplorer.application")
objIE.visible = true
objIE.navigate "https://data.biq.dk/users/sign_in" '<--make sure the correct path is entered here(the one which takes you to that page containing the table)
while objIE.readyState <>4
Wscript.sleep 1000
Wend
set objTable = objIE.document.getElementById("calculations_data")
strxml = objTable.innerhtml
set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async=False
objXML.loadxml strxml
set objRowNodes = objXML.selectnodes("//tr")
for i=1 to objRowNodes.length-1
tempArr = split(objRowNodes(i).text)
msgbox "second last: "&tempArr(ubound(tempArr)-1)&vbcrlf&_ 'Displays the 2nd last column data
"last: "&tempArr(ubound(tempArr)) 'Displays the last column data
next
Output:

Using Html Agility Pack to capture inner text from a specific node

I am trying to grab the text "Record No: 1" and the two dates from the following html snippet:
<table class="Report">
<tbody>
<tr>
<td>
<font><b>Record No: 1</b><br>
<i>Original Date</i>: 12/16/2011<br>
<i>Original Entered Date</i>: 12/16/2011
<br>
<br>
</font>
</td>
</tr>
</tbody>
<table>
Using HTMLAgilityPack and the following code I've been able to get the record number but am not sure how to grab the dates.
var recordNum =report.Descendants()
.Where(a=>a.InnerText.Contains("Record No:"))
.Where(a => a.Name == "#text")
.First().InnerText;
Somehow I need to be able to grab the text following the "Original Date" node.
Somehow I need to be able to grab the text following the "Original Date" node.
You can use the following XPath to select text nodes located after i element where inner text equals 'Original Date' :
//i[.='Original Date']/following-sibling::text()
Use the XPath as follow, for example :
var doc = new HtmlDocument();
....
var xpath = "//i[.='Original Date']/following-sibling::text()";
var result = doc.DocumentNode.SelectSingleNode(xpath);
Console.WriteLine(result.InnerText);
Demo
output :
: 12/16/2011

VLOOKUP using batch scripting

I am new to scripting and perform lot of activity as an analyst using excel sheets.
I have two files with list of items in it.
File1 contains 1 column
File2 contains 2 columns.
I want to check if the list present in column1 of file2 is same as in column1 of file2. If yes then it should print column1File1, column1File2 and coulmn2File2 in file3 else it should print "NA", column1File2, column2File2 in file3.
Please help, It will simplify my work a lot.
I made this program a while ago, although it will iterate through sheets in 1 workbook, and compare cell by cell, it may set you in the right direction. It would take a cell in 1 "master" sheet and then iterate through each sheet to find that in a particular column. After it found it the counter would increment, then it would take the next cell in the master sheet and so on. you could alter to use multiple books and take whatever cells you want and compare them.
Sub Open_Excel()
'Use worksheetNum as sheet to read/write data
Set currentWorkSheet = objExcel.ActiveWorkbook.Worksheets(worksheetNum)
'How many rows are used in the current worksheet
usedRowsCount = currentWorkSheet.UsedRange.Rows.Count
'Use current worksheet cells for values
Set Cells = currentWorksheet.Cells
'Loop through each row in the worksheet
For curRow = startRow to (usedRowsCount)
'Get computer name to ping to
strEmailAddressSource = Cells(curRow,colEmailAddressSource).Value
strServerSource = Cells(curRow,colHostserverSource).Value
strLocationSource = Cells(curRow,colLocationSource).Value
'make the values unique
strconcatenation = strServerSource & strLocationSource
Call Comparison()
Next
End Sub
'********************************************************************************************
'**** Comparison
'********************************************************************************************
'Comparison test
Sub Comparison()
'choose the worksheets to go through
For worksheetCounter = 6 to 9 'workSheetCount
Set currentWorkSheetComparison = objExcel.ActiveWorkbook.Worksheets(worksheetCounter)
usedRowsCountNew = currentWorkSheetComparison.UsedRange.Rows.Count
'First row to start the comparison from
For rowCompare = 2 to (usedRowsCountNew)
strEmailLot = currentWorkSheetComparison.Cells(rowCompare,colEmailAddressLot).Value
comp1 = StrComp(strEmailAddressSource,strEmailLot,0)
comp2 = StrComp(strconcatenation,reportConcat,0)
'check if the values match
If ((comp1 = 0) AND (comp2 = 0)) THEN
countvalue = countvalue + 1
End If
Next
Next
End Sub

Ruby - extracting table values

I'm trying to parse a value from an HTML table (below) using ruby, watir and regular expressions. I want to parse the id info from the anchor tag if the table rows have specified values. For example, if Event1, Action2 are my target row selections, then my goal is to get the "edit_#" for the table row.
Table:
Event1 | Action2
Event2 | Action3
Event3 | Action4
Example of HTML (I've cut some info out since this is work code, hopefully you get the idea):
<div id="table1">
<table class="table1" cellspacing="0">
<tbody>
<tr>
<tr class="normal">
<td>Event1</td>
<td>Action2</td>
<td>
<a id="edit_3162" blah blah blah… >
</a>
</td>
</tr>
<tr class="alt">
<td> Event2</td>
<td>Action3 </td>
<td>
<a id="edit_3163" " blah blah blah…>
</a>
</td>
</tr>
</tbody>
</table>
</div>
I have tried the following that doesn't really work:
wb=Watir::Browser.new
wb.goto "myURLtoSomepage"
event = "Event1"
action = "Action2"
table = browser.div(:id, "table1")
policy_row = table.trs(:text, /#{event}#{action/)
puts policy_row
policy_id = policy_row.html.match(/edit_(\d*)/)[1]
puts policy_id
This results in this error which is pointing to the policy_id = ...
line : undefined method 'html' for #<Watir::TableRowCollection:0x000000029478f0> (NoMethodError)
Any help is appreciated as I am fairly new to ruby and watir.
Something like this should work:
browser.table.trs.each do |tr|
p tr.a.id if tr.td(:index => 0) == "Event1" and tr.td(:index => 1) == "Action2"
end
This is an alternative to Željko answer. Assuming there is only one row that matches, you can use find instead of each to only iterate through the rows until the first match is found (rather than always going through every row).
#The table you want to work with
table = wb.div(:id => 'table1').table
#Find the row with the link based on its tds
matching_row = table.rows.find{ |tr| tr.td(:index => 0).text == "Event1" and tr.td(:index => 1).text == "Action2" }
#Get the id of the link in the row
matching_row.a.id

Resources