简体   繁体   English

如何在表中搜索值,然后在同一行上获取其他单元格值?

[英]How can I search a value in a table and then get the other cell value on the same row?

I want to write a generic method, which I can do a search on a table to locate a specific row. 我想编写一个通用方法,可以在表上进行搜索以找到特定的行。 Then, I can get the other values on the same row. 然后,我可以在同一行上获取其他值。

<table>
   <tr>
      <td>Amazon</td>
      <td>$100.00</td>
   </tr>
   <tr>
      <td>Samsung</td>
      <td>$200.00</td>
   </tr>
</table>

For example, I pass amazon to the method. 例如,我将亚马逊传递给该方法。 The method will locate the row contains Amazon. 该方法将找到包含Amazon的行。 And, it will get the value = $100.00 on the same row. 并且,它将在同一行上获得= $ 100.00的值。

How can i do it using Selenium Java? 我如何使用Selenium Java? Thanks 谢谢

The following method may be help you. 以下方法可能会对您有所帮助。

public String find(WebDriver driver, String searchString){
    List<WebElemet> lstElements=driver.findElement(By.xpath("//table/tr/td");
       for(int i=0;i<lstElements.size();i++){
         if(lstElements.get(i).getText().equals(searchString)){
           return lstElments(i+1).getText();
      }
}

You can use below code to get your value from table pass amazon into str then you will get respective value as $100.00 您可以使用以下代码从表格传递到str的值中获取您的价值,然后您将分别获得$ 100.00的价值。

   public String getValue(String str){
                        String xpath = "//td[text()='%s']";
                          String ele= String.format(xpath,str);
                            WebElement productname= driver.findElement(By.xpath(ele));
               return productname.findElement(By.xpath("/following-sibling::td")).getText(); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从HTML表格中的选定行获取单元格值? - How to get cell value from selected row in HTML table? 如何使用单元格值搜索行索引 - How to search row index using cell value 在不按Enter或Tab键的情况下,如何获取表格单元格值为“ xyz”? - How can I get table cell value as “xyz” WITHOUT hit the Enter or Tab key? 如何使用复选框(JSP 和 HTML)从表格行中获取值? - How can I get value from one table row using checkbox ( JSP & HTML )? 如何显示一个表中下拉框的第一个值和另一个表中相同下拉列表的其他值 - How can I display first value of drop down box from one table and other values of same drop down from another table 如何从 excel 文件中获取特定的单元格值 - How can I get a specific cell value from an excel file Android ListView 如何获取每一行的值 - Android ListView how can I get the value of each individual row 如何获取JTable中显示的单元格值(不是完整的TableModel值)? - How can i get the displayed cell value in a JTable (not full TableModel value)? Java Swing:在JTable中编辑单元格时如何获取上一个值和当前值? - Java Swing: How can I get the previous value and the current value when editing a cell in JTable? 如何将日期值设置到单元格 - How can I set Date value into cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM