nokogiri parsing first td in tr ignoring specific class - ruby

I have the following html
<table>
<tr>
<th>value</th>
<th>description</th>
</tr>
<tr>
<td>OverallHealthScore</td>
<td>
Overall HealthScore.
</td>
</tr>
<tr>
<td class="deprecated">DESTAGED_TRACKS_PER_SEC</td>
<td>
The tracks per second saved into disks.
</td>
</tr>
</table>
There are many many tr's but this is an excerpt of the two scenario's
I need to only print out OverallHealthScore
table.css('tr').map do |row|
puts row.css('td:not(.deprecated)').map(&:text)[0]
end
Gets me just about there but prints out the "description" td on the deprecated items. I can't seem to figure out what I need to do in order to get the results I am needing.

Assuming you want to get the first td's value which are not deprecated:
<table>
<tr>
<th>value</th>
<th>description</th>
</tr>
<tr>
<td>OverallHealthScore</td>
<td>
Overall HealthScore.
</td>
</tr>
<tr>
<td class="deprecated">DESTAGED_TRACKS_PER_SEC</td>
<td>
The tracks per second saved into disks.
</td>
</tr>
<tr>
<td>AvaiableAnother</td>
<td>
Another Available HealthScore.
</td>
</tr>
<tr>
<td class="deprecated">OTHER_DEPRE</td>
<td>
The tracks per second saved into disks.
</td>
</tr>
</table>
Then
puts table.css('td:first-child:not(.deprecated)').map(&:text)
# OverallHealthScore
# AvaiableAnother

Related

List only records populated with Capybara

Friends helped me with a solution that validates if there are [active/inactive] records in the list. When I list the records using pp capybara also returns blank lines. How do I disregard empty records?
def validate_active_inactive_records
expect(page).to have_css("td:nth-child(5)", :text => /^(ACTIVE|INACTIVE)$/)
# ***listing records***
page.all('.tvGrid tr > td:nth-child(5)').each do |td|
puts td.text
end
end
<table width="100%" class="tvGrid">
<tbody>
<tr>
<th colspan="1" class="tvHeader">Id</th>
<th colspan="1" class="tvHeader">Code</th>
<th colspan="1" class="tvHeader">Description</th>
<th colspan="1" class="tvHeader">Operational Center</th>
<th colspan="1" class="tvHeader">Status</th>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="tvRowEmpty">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
Are you asking how to remove the rows with the class tvRowEmpty from your search results? If so, you can use the :not operator in your finder:
def validate_active_inactive_records
expect(page).to have_css("td:nth-child(5)", :text => /^(ACTIVE|INACTIVE)$/)
# ***listing records***
page.all('.tvGrid tr:not(.tvRowEmpty) > td:nth-child(5)').each do |td|
puts td.text
end
end
If you want to exclude any td that just contains you could use the following finder with a regex that filters tags containing only whitespace characters:
page.all('.tvGrid tr > td:nth-child(5)', text: /[\s]^*/).each

Correct mrtg cfgmaker file

mrtg cfgmaker does read incorrect values over SNMP V1 and V2 and I need to correct the resulting file.
I would like to run a script after creation and use sed if possible.
Lines that needs to be corrected in my case are for LAG's and normal ports:
MaxBytes[switch01_lag_26]: 125000000 should go to MaxBytes[switch01_lag_26]: 250000000
(switch01_lag_26 can be switch01_lag_1 until switch01_lag_26)
MaxBytes[switch01_g1]: 12500000 should go to MaxBytes[switch01_g1]: 125000000
(switch01_g1 can be switch01_g1 until switch01_g16)
What sed patterns I have to use to analyze if its a lag or port in the square brackets and then replace the number after the : ?
The html part should show the correct speed if possible too, this is original for port g1:
<h1>Traffic Analysis for g1-- switch01</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>switch01</td>
</tr>
<tr>
<td>Maintainer:</td>
<td></td>
</tr>
<tr>
<td>Description:</td>
<td>1-Gigabit---Level </td>
</tr>
<tr>
<td>ifType:</td>
<td>ethernetCsmacd (6)</td>
</tr>
<tr>
<td>ifName:</td>
<td>g1</td>
</tr>
<tr>
<td>Max Speed:</td>
<td>12.5 MBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>No Ip (No DNS name)</td>
</tr>
</table>
</div>
and should read at the end (Line below "Max Speed" is changed):
<h1>Traffic Analysis for g1-- switch01</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>switch01</td>
</tr>
<tr>
<td>Maintainer:</td>
<td></td>
</tr>
<tr>
<td>Description:</td>
<td>1-Gigabit---Level </td>
</tr>
<tr>
<td>ifType:</td>
<td>ethernetCsmacd (6)</td>
</tr>
<tr>
<td>ifName:</td>
<td>g1</td>
</tr>
<tr>
<td>Max Speed:</td>
<td>125.0 MBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>No Ip (No DNS name)</td>
</tr>
</table>
</div>
This is original for LAG 1:
<h1>Traffic Analysis for lag 1 -- switch01</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>switch01</td>
</tr>
<tr>
<td>Maintainer:</td>
<td></td>
</tr>
<tr>
<td>Description:</td>
<td>lag-1 </td>
</tr>
<tr>
<td>ifType:</td>
<td>IEEE 802.3ad Link Aggregate (161)</td>
</tr>
<tr>
<td>ifName:</td>
<td>lag 1</td>
</tr>
<tr>
<td>Max Speed:</td>
<td>125.0 MBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>No Ip (No DNS name)</td>
</tr>
</table>
</div>
which should read at the end (Line below "Max Speed" is changed):
<h1>Traffic Analysis for lag 1 -- switch01</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>switch01</td>
</tr>
<tr>
<td>Maintainer:</td>
<td></td>
</tr>
<tr>
<td>Description:</td>
<td>lag-1 </td>
</tr>
<tr>
<td>ifType:</td>
<td>IEEE 802.3ad Link Aggregate (161)</td>
</tr>
<tr>
<td>ifName:</td>
<td>lag 1</td>
</tr>
<tr>
<td>Max Speed:</td>
<td>250.0 MBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>No Ip (No DNS name)</td>
</tr>
</table>
</div>
I can change all speeds in HTML using sed -i 's/\([0-9.]\+\) MBytes/125.0 MBytes/' /switch01.cfg but this changes for LAG's too. How to detect if the HTML part belongs to a LAG?

Protractor expect not exist in page

I have html page like this:
<table>
<thead>
<tr>
<td>title</td>
<td>desc</td>
<td>status</td>
</tr>
</thead>
<tbody>
<tr>
<td><label>lorem1</label></td>
<td><label>desc1 lorem</label></td>
<td><label>active</label></td>
<td> Delete </td>
</tr>
<tr>
<td><label>lorem2</label></td>
<td><label>desc2 lorem</label></td>
<td><label>active</label></td>
<td> Delete </td>
</tr>
<tr>
<td><label>lorem3</label></td>
<td><label>desc3 lorem</label></td>
<td><label>deactive</label></td>
<td> Delete </td>
</tr>
</tbody>
</table>
Now I delete record lorem2 from above list (with click on delete link) and after that I want to check lorem2 that deleted shouldn't exist or contain in page.
I write this code but it's not correct:
expect(element(by.css("table")).getText()).not.toBe('lorem2');
You will delete the lorem2 by a locator may be xpath
below for deleting
//tr/td//label[contains(text(),"lorem2")]/following::td/a
below for checking if exist after deletion
//tr/td//label[contains(text(),"lorem2")]
you should parameterize xpath (i.e) the text Lorem2 for other text.
expect(element(by.xpath('//tr/td//label[contains(text(),"lorem2")]
')).isPresent()).toBe(false);

How to grab all the elements inside a table?

I have a HTML in the format of this
<tbody>
<tr>
<td> Test1 </td>
<td> .. </td>
<tr/>
<tr> ... </tr>
</tbody>
<tbody>
<tr>
<td> Test2 </td>
<td> .. </td>
<tr/>
<tr> .. </tr>
</tbody>
<tbody>
<tr>
<td> Test3 </td>
<td> .. </td>
<tr/>
<tr> .. </tr>
</tbody>
How can I iterate through all the tbody's and grab the text inside the tr/td? I tried to do the following:
items = driver.find_elements(:xpath, "//tbody/tr[1]/td[1]").map(&:text)
puts items
and
items = driver.find_elements(:xpath, ".//tbody//td[1]").map(&:text)
puts items
In both of these cases, puts is empty (its reaching that point in code I checked). How can I grab all the items inside of the tbody/tr/td?

tfooter doesn't validate for xhtml?

I had my webpage validated for xhtml transitional till I added this table (see below). Since then it doesn't validate and says "
document type does not allow element "tfoot" here <tfoot>
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).
One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error)."
Any ideas as what is happening? I checked for any opened and not closed tags but did not find any so I don't know what else is wrong.
<table>
<caption>
My first table, Anna
</caption>
<thead>
<tr>
<th>
June
</th>
<th>
July
</th>
<th>
August
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Data 1
</td>
<td>
Data 2
</td>
<td>
Data 3
</td>
<td>
Data 4
</td>
</tr>
<tr>
<td>
Data a
</td>
<td>
Date b
</td>
<td>
Data c
</td>
<td>
Data d
</td>
</tr>
<tfoot>
<tr>
<td>
Result1
</td>
</tr>
</tfoot>
</tbody>
</table>
You've got the <tfoot> at the end of the table. It should be between the <thead> and the <tbody>. It will appear at the bottom, but it's coded at the top. One of the original ideas is that as a large table loaded, the heading and footer would be visible quickly, with the rest filling in (esp. useful if the body was scrollable between them). It hasn't quite worked out like that in practice, but it does make more sense if you know that.
In the DTD it lists:
<!ELEMENT table (caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))>
That is, optional caption, then zero-or-more col or colgroup, then optional thead, then optional tfoot, then at least one tbody or tr.
UPDATE: Note that HTML 5 now allows one to put the <tfoot> at the end of the table, instead of before the first <tbody> (or the first <tr> that isn't in a <thead>, <tfoot> or <tbody> and hence in a single implicit <tbody>). As such the code in the question would now be considered valid. The older approach is also still valid, and probably advisable.
The tfoot element should be outside of the tbody element, like this:
<table>
<caption>
My first table, Anna
</caption>
<thead>
<tr>
<th>
June
</th>
<th>
July
</th>
<th>
August
</th>
</tr>
</thead>
<tfoot>
<tr>
<td>
Result1
</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>
Data 1
</td>
<td>
Data 2
</td>
<td>
Data 3
</td>
<td>
Data 4
</td>
</tr>
<tr>
<td>
Data a
</td>
<td>
Date b
</td>
<td>
Data c
</td>
<td>
Data d
</td>
</tr>
</tbody>
Here is a small example of the correct nesting for those who need it.
<table>
<caption></caption>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>

Resources