【问题标题】:Xpath with combination of Preceding-sibling and following-siblingXpath 结合了 Preceding-sibling 和 following-sibling
【发布时间】:2018-06-15 12:36:07
【问题描述】:

我正在尝试从此屏幕读取值(部分动态显示,可能不止一个)。我们必须阅读每个字段,例如本地无线电、MAC 地址、通用应用程序统计中的版本和旧版配置中的默认地理代码。我得到了 xapth 来确定显示了多少部分。但无法阅读每个部分下的内容。我必须像键/值对一样阅读它们。由于 html 的结构(下)我很难在两个部分之间编写 xpath,例如 ...General Application Statistics 和 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。我确定它有问题,但有人可以指导我如何解决这个问题..

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

【问题讨论】:

  • xpath between two section 到底是什么意思?您要选择/单击哪个元素?
  • 我正在尝试获取我发布的 html 中两个部分之间的元素。例如在一般应用统计部分。有本地无线电、MAC 地址、版本。那些我作为键和各自的值进入字典(机器人框架)。所以我试图在 xpath 的帮助下获取两个部分之间的元素..(最初我需要 count 来查看键的数量)
  • findElement() 将只返回一个节点,而findElements() 将返回一个严格遵循xpath 的节点的List。如何获取具有 n 个不同标签的 all the elements between two sections in the html
  • 那么您能更正 xpath 吗?我正在尝试获得一个,但有些失败或
  • 我无法正确理解您的Usecase。您尝试自动化的具体手动步骤是什么?

标签: selenium xpath robotframework


【解决方案1】:

要从 General Application Statistic 中检索每个字段(如 Local RadiosMAC AddressVersion)和 Legacy Configuration 中的字段 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));

【讨论】:

  • 我现在要试试这个
  • 我保持xpath 灵活检测字符串General Application StatisticLegacy Configuration。当您通过 General Application Statistic 时,ExpectedConditions 将是 numberOfElementsToBe(By(xpath), 6),当您通过 Legacy Configuration 时,ExpectedConditions 将是 numberOfElementsToBe(By(xpath), 2)。在这两种情况下,只有String(通用应用程序统计/旧版配置)和元素数量发生了变化(6/2)。
  • List all_items = new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfElementsToBe(By.xpath("//table[@class='tabletext']/tbody//tr/ /td//table//tbody//tr//td[contains(.,'General Application Statistics')]//following::td"), 6));返回 11 项。我只想获取三个字段。
  • 如果我只想获取本地无线电、MAC 地址和版本等字段标签怎么办。 xpath应该是什么?你能建议吗
  • 这有助于获取属性标签和值
【解决方案2】:

既然你已经标记了Robot Framework,我在这里给出一个机器人特定的解决方案:

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

对于您想要的所有其他字段也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多