Xpath with combination of Preceding-sibling and following-sibling

限于喜欢 提交于 2019-12-02 09:44:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!