Xpath 结合 Preceding-sibling 和 Follow-sibling [英] Xpath with combination of Preceding-sibling and following-sibling

查看:66
本文介绍了Xpath 结合 Preceding-sibling 和 Follow-sibling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此屏幕读取值(动态显示的部分可能不止一个).我们必须读取每个字段,如本地无线电、MAC 地址、来自常规应用统计的版本和来自传统配置的默认地理代码......我得到了 xapth 来确定显示了多少个部分.但无法阅读每个部分下的内容.我必须像键/值对一样阅读它们.. 由于 html 的结构(如下),我很难在两个部分之间编写 xpath,例如 ...General Application统计和旧配置

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>

这是我正在尝试修复的 xpath.我确定它有问题,但有人可以指导我如何解决这个问题..

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

推荐答案

检索每个字段,如Local RadiosMAC AddressVersion来自 General Application Statistic 和来自 Legacy Configuration 的字段 Default Geo Code,您可以使用以下代码块:

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

这篇关于Xpath 结合 Preceding-sibling 和 Follow-sibling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆