Xpath with combination of Preceding-sibling and fo

2019-03-05 15:36发布

问题:

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']]

回答1:

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));


回答2:

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.