Xpath with combination of Preceding-sibling and following-sibling - selenium

I am trying to read the values from this screen( Sections appears dynamically it can be more than one). We have to read each field like Local Radios, MAC Address, Version from General Application Statistic and Default Geo Code from Legacy Configuration.. i got xapth to identify how many sections displayed. But unable to read the content under each section. i have to read them like key/value pair.. Due to the structure of html(Below) I am having tough time to write xpath between two section like ...General Application Statistics and Legacy Configuration
<table class="tabletext">
<tbody>
<tr>
<td style="font-weight:bold; font-size:large">Collector Information</td>
</tr>
</tbody>
</table>
<table class="tabletext">
<tbody>
<tr>
<td style="font-size:medium" colspan="2" align="left">
<table>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td style="color:Navy; font-size:20px; border-bottom: solid 1px black; " width="700px">General Application Statistics</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="font-weight:bold; font-size:medium">Local Radios</td>
<td align="left">AA.5E.AZ.21.13.04[Z0136FBZ]</td>
</tr>
<tr>
<td style="font-weight:bold; font-size:medium">MAC Address</td>
<td align="left">91-99-99-0C-66-B2</td>
</tr>
<tr>
<td style="font-weight:bold; font-size:medium">Version</td>
<td align="left">14.48.24.0</td>
</tr>
<tr>
<td style="font-size:medium" colspan="2" align="left">
<table>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td style="color:Navy; font-size:20px; border-bottom: solid 1px black; " width="700px">Legacy Configuration</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="font-weight:bold; font-size:medium">Default Geo Code</td>
<td align="left">AF.ZA.QE.23.23.1F</td>
</tr>
</tbody>
</table>
This is the xpath I am trying to fix.I am sure its buggy but can some one direct me how to resolve this..
//table[2]/tbody/tr[td/table/tbody/tr[2]/td[normalize-space(text())='Legacy Configuration']]/preceding-sibling::tr and following-sibling::tr[td/table/tbody/tr[2]/td[normalize-space(text())='General Application Statistics']]

To retrieve each field like Local Radios, MAC Address, Version from General Application Statistic and the field Default Geo Code from Legacy Configuration you can use the following code block :
List<WebElement> all_items1 = driver.findElements(By.xpath("//table[#class='tabletext']/tbody//tr//td//table//tbody//tr//td[contains(.,'General Application Statistics')]//following::td"));
List<String> properties = new ArrayList<String>(3);
List<String> values = new ArrayList<String>(3);
for (int i=0;i<all_items.size();i=i+2)
properties.add(all_items.get(i).getAttribute("innerHTML"));
for (int j=1;j<all_items.size();j=j+2)
values.add(all_items.get(j).getAttribute("innerHTML"));
for (int k=0;k<properties.size();k++)
System.out.println("Property " + properties.get(k) + " has a value of " + values.get(k));
List<WebElement> all_items2 = driver.findElements(By.xpath("//table[#class='tabletext']/tbody//tr//td//table//tbody//tr//td[contains(.,'Legacy Configuration')]//following::td"));
List<String> properties = new ArrayList<String>(1);
List<String> values = new ArrayList<String>(1);
for (int i=0;i<all_items.size();i=i+2)
properties.add(all_items.get(i).getAttribute("innerHTML"));
for (int j=1;j<all_items.size();j=j+2)
values.add(all_items.get(j).getAttribute("innerHTML"));
for (int k=0;k<properties.size();k++)
System.out.println("Property " + properties.get(k) + " has a value of " + values.get(k));

Since you have tagged Robot Framework, i'll give a robot specific solution here:
${legal_key}= Get Text xpath://td[text()='Local Radios']
${legal_value}= Get Text xpath://td[text()='Local Radios']/following-sibling::td
Similarly for all the other fields you want.

Related

Replicating a table

I'm having trouble replicating this table. I'm confused with the rowspan and colspan. I would really appreciate it if someone helps.
Click here to view image of the table
Here you go:
<table>
<tr>
<td colspan="7">The Error Rate on Different Forms</td>
</tr>
<tr>
<td rowspan="2">Form name</td>
<td rowspan="2">A</td>
<td rowspan="2">B</td>
<td rowspan="2">Total Fields (X = A x B)</td>
<td rowspan="2">Fields with errors (Y)</td>
<td colspan="2">Error rate* (Y/X) x 100 (%)</td>
</tr>
<tr>
<td>X/Y</td>
<td>%age</td>
</tr>
<tr>
<td colspan="7">High Risk Errors</td>
</tr>
<!-- normal tr td -->
<tr>
<td colspan="7">Low Risk Error</td>
</tr>
<!-- normal tr td -->
</table>

Is <tbody> automatically defined in a table?

I'm a starter and I was trying some elements, as practice.
There's sth that i can't figure it out.I also have searched google but no answers found.
I wrote 2 tables, in the first one I used tbody to style the body of the table. But when I load the page, I see the style used in css for the tbody in the first table, has also effected the second table(without tbody tag) completely.Why is this happening?
Here is the code :
<table id="t1">
<caption>UFO Seen by People</caption>
<thead>
<tr>
<th>Name</th>
<th>City Name</th>
<th>Seen</th>
<th>Times seen</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">Jack</td>
<td>North Russia</td>
<td>2020-06-12</td>
<td>once</td>
</tr>
<tr>
<td>North Korea</td>
<td>2000-06-12</td>
<td>once</td>
</tr>
<tr>
<td>North Pole</td>
<td>1995-06-12</td>
<td>twice</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">Blah Blah Blah</td>
</tr>
</tfoot>
</table>
<table id="t2">
<caption>UFO Seen by People2</caption>
<colgroup>
<col span="2" style="text-align:right; background-color:yellow;">
<col style="background-color:cyan; background-image:url('baelen.jpg');">
</colgroup>
<tr>
<th>Name</th>
<th>City Name</th>
<th>Seen</th>
<th>Times seen</th>
</tr>
<tr>
<td rowspan="3">Dan</td>
<td>North Russia</td>
<td>2020-06-12</td>
<td>once</td>
</tr>
<tr>
<td>North Korea</td>
<td>2000-06-12</td>
<td>once</td>
</tr>
<tr>
<td>North Pole</td>
<td>1995-06-12</td>
<td>twice</td>
</tr>
</table>
And here is the css used :
#t1 { border:2px solid black; border-spacing:10pt; background-color:pink; width:100%;}
#t2 { border:2px solid rgb(20,20,20); border-collapse:collapse; width:100%;}
tbody { background-color:white; text-align:center; vertical-align:middle; font-size:20pt;}
That's really simple, that affect the second table cause you use tbody in css and not #t1 tbody, use correct selector if you want to affect only one element.
In addition I advise you not to use style on HTML and in CSS except in the case of manipulation in JavaScript. This will also allow you to better understand what does what, if you want to give a particular style to elements of your table assign their classes to your CSS.

image uploaded to database but unable to display the image

Im using the simpleform to upload the file image
<tr>
<td align="right">Photo</td>
<td><input type="file"size="30" name="photo" /></td>
</tr>
then here is my php file..Im using mysql_fetch_array
row[6] is my photo...
// retrieves a row data and returns it as an associative array
while($row=mysql_fetch_array($result)){
echo "<table border='1' align='center' class='table_background'>
<tr>
<td align='left' width=100>UserName=</td>
<td align='left' width='400'></td>
</tr>
<tr>
<td align='left' width=100>Title=</td>
<td align='left' width='400'>$row[1]</td>
</tr>
<tr>
<td align='left'width=100>Category=</td>
<td align='left'width='400'>$row[2]</td>
</tr>
<tr>
<td align='left'width=100>Description=</td>
<td align='left'width='400'>$row[3]</td>
</tr>
<tr>
<td align='left'width=100>State=</td>
<td align='left'width='400'>$row[4]</td>
</tr>
<tr>
<td align='left'width=100>Photo=</td>
<td align='left'width='400'>$row[5]</td>
</tr>
<tr>
<td align='left'width=100>Date=</td>
<td align='left'width='200'>$row[6]</td>
</tr>
<br>
}
<br>
<br>
</table>";
}
In my database I use Blob as the type of image ... the image is successful upload to my database
but just display the image file name...
blob is a string so you have to display id a bit different
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row[6] ).'"/>';

vb.net: Get data from Xelement <table></table>

I have loaded a Xelement from my SharePoint site.
Dim elTable2 As XElement = <table border="1" id="table2" style="font-size:1em;border-collapse:collapse;display:inline;width:100%">
<tbody>
<tr class="ms-rteTableHeaderRow-default" style="text-align:center">
<th class="ms-rteTableHeaderFirstCol-default">​</th>
<th class="ms-rteTableHeaderOddCol-default">LAN IP​</th>
<th class="ms-rteTableHeaderEvenCol-default">Username​</th>
<th class="ms-rteTableHeaderOddCol-default">Password​</th>
<th class="ms-rteTableHeaderEvenCol-default">Port​</th>
<th class="ms-rteTableHeaderOddCol-default">OS​</th>
<th class="ms-rteTableHeaderEvenCol-default">Extra Info​</th>
</tr>
<tr class="ms-rteTableOddRow-default" style="text-align:center">
<th class="ms-rteTableFirstCol-default">Netasq</th>
<td class="ms-rteTableOddCol-default"></td>
<td class="ms-rteTableEvenCol-default"></td>
<td class="ms-rteTableOddCol-default">​</td>
<td class="ms-rteTableEvenCol-default">​</td>
<td class="ms-rteTableOddCol-default">​</td>
<td class="ms-rteTableEvenCol-default">​</td>
</tr>
</tbody>
</table>
The table will have an unknown number of rows. Can I loop trough all the rows and check what data is in the first column?
You could leverage Html Agility Pack for that purpose. It is a .NET code library that allows to parse HTML content and supports plain XPATH or XSLT.
Example
Dim table As XElement = <table border="1" id="table2" style="font-size:1em;border-collapse:collapse;display:inline;width:100%">
<tbody>
<tr class="ms-rteTableHeaderRow-default" style="text-align:center">
<th class="ms-rteTableHeaderFirstCol-default">​Val</th>
<th class="ms-rteTableHeaderOddCol-default">LAN IP​</th>
<th class="ms-rteTableHeaderEvenCol-default">Username​</th>
<th class="ms-rteTableHeaderOddCol-default">Password​</th>
<th class="ms-rteTableHeaderEvenCol-default">Port​</th>
<th class="ms-rteTableHeaderOddCol-default">OS​</th>
<th class="ms-rteTableHeaderEvenCol-default">Extra Info​</th>
</tr>
<tr class="ms-rteTableOddRow-default" style="text-align:center">
<td class="ms-rteTableFirstCol-default">Netasq</td>
<td class="ms-rteTableOddCol-default"></td>
<td class="ms-rteTableEvenCol-default"></td>
<td class="ms-rteTableOddCol-default">​</td>
<td class="ms-rteTableEvenCol-default">​</td>
<td class="ms-rteTableOddCol-default">​</td>
<td class="ms-rteTableEvenCol-default">​</td>
</tr>
</tbody>
</table>
Dim html = New HtmlDocument()
html.LoadHtml(table.ToString())
For Each row In html.DocumentNode.SelectNodes("//tr[position()>1]") 'XPath expression to select table rows and skip table header
Dim cell As HtmlNode = row.SelectSingleNode("td[1]") 'Get cell for first column
Console.WriteLine(cell.InnerText)
Next

Selenium - pulling data from a website table assign to variable

I am attempting to pull a value and a header (string) from a website, but unable to find the element using selenium.
My Code
I used Firebug to get the XPath and this is what it determined:
//*[#id="DimensionForm"]/p[1]/table/tbody/tr[3]/td[3]
Code
Dim Right as double
Dim Marker as string
Marker = selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[2]/td[3]").getAttribute("value")
Right = selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[3]/td[3]").getAttribute("value")
HTML CODE
<form id="DimensionForm" name="validate" action="Dimension" method="post">
<div style="margin-top: 7px"></div>
<p><table width="100%" cellspacing="0" border="0" cellpadding="0" class="element">
<tr>
<td> </td><td class="formtitlenobg" colspan="6" align='right'>
AREA DIMENSIONS (AREA A) <span class='quote'> Front</span> 25.24</td>
</tr>
<tr align="right">
<td class="tablerowlightgreen" width=10> </td>
<th class="formtitle" width=250 align="left">Property</th>
<th class="formtitle" width=50>Check</th> <th class="formtitle" width=75>Front</th>
<th class="formtitle" width=75>Center</th><th class="formtitle" width=75>Left</th>
<th class="formtitle" width=120>Right</th>
<th class="formtitle" width=100>Total</th>
<td class="tablerow" width=50> </td>
<td class="tablerow"> </td>
</tr>
<tr align="right" nowrap>
<td> </td>
<td class="table" align="left"><strong>
Property O</strong></td>
<td class="table">+</td>
<td class="table">10</td>
<td class="table">12</td>
<td class="table"><strong>12</strong></td>
<td class="table"><strong><font class="front">
100</font></strong></td>
<td class="table">120</td>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr></table>
You have incorrectly nested quotes:
selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[2]/td[3]")
Perhaps you meant:
selenium.findElementByXPath("//*[#id='DimensionForm']/p[1]/table//tr[2]/td[3]")
Note the single-quotes in the second line!