【问题标题】:Getting the middle column value with the other two column values using xpath in web table使用 web 表中的 xpath 获取中间列值和其他两列值
【发布时间】:2021-05-13 22:00:38
【问题描述】:

<tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>

[https://www.w3schools.com/html/html_tables.asp]

您可以在链接中找到示例网络表格。我想要与公司和国家联系的价值。

我尝试过类似的方法

//td[text()='Laughing Bacchus Winecellars']//following-sibling::td and //td[text()='Canada']//preceding-sibling::td

【问题讨论】:

    标签: selenium selenium-webdriver xpath ui-automation browser-automation


    【解决方案1】:

    试试这个选择需要的td节点:

    //td[preceding-sibling::td[.="Alfreds Futterkiste"] and following-sibling::td[.="Germany"]]
    

    【讨论】:

      【解决方案2】:

      您可以循环通过&lt;tr&gt; 并使用//tr/td[2] 选择第二个&lt;td&gt;

      http://xpather.com/aHyybZni

      【讨论】:

        【解决方案3】:

        尝试以下 xpath:

        //td[preceding-sibling::td[text()='Laughing Bacchus Winecellars'] and following-sibling::td[text()='Canada']]
        

        【讨论】:

          【解决方案4】:

          要提取中间列值,即Maria Anders,您需要为visibilityOfElementLocated() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

          • 使用公司文本Alfreds Futterkiste

            • xpath:

              //table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]
              
            • 代码行:

              System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Alfreds Futterkiste']//following::td[1]"))).getText());
              
          • 使用国家文本德国

            • xpath:

              //table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]
              
            • 代码行:

              System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@id='customers']//tr//td[text()='Germany']//preceding::td[1]"))).getText());
              

          【讨论】:

            猜你喜欢
            • 2021-11-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多