结合了Preceding-sibling和follow-sibling的Xpath

时间:2018-01-06 07:04:04

标签: selenium xpath robotframework

我正在尝试从此屏幕中读取值(章节动态显示,它可以是多个)。我们必须读取每个字段,如本地无线电,MAC地址,来自常规应用统计的版本和来自旧版配置的默认地理代码..我得到了xapth来识别显示的部分。但无法阅读每个部分下的内容。我必须像键/值对一样阅读它们。enter image description here由于html(下面)的结构,我很难在两个部分之间编写xpath,如...一般应用程序统计信息和旧版配置

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

这是我试图修复的xpath。我确定它的错误,但有人可以指导我如何解决这个问题。

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

2 个答案:

答案 0 :(得分:1)

常规应用统计中检索Local RadiosMAC AddressVersion旧版配置中的字段Default Geo Code等每个字段您可以使用以下代码块:

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

答案 1 :(得分:0)

由于您已标记机器人框架,因此我将在此处提供针对机器人的解决方案:

${legal_key}=      Get Text    xpath://td[text()='Local Radios']
${legal_value}=    Get Text    xpath://td[text()='Local Radios']/following-sibling::td

与您想要的所有其他字段类似。