Java Selenium WebDriver:帮助从表中获取特定行数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19313385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:00:31  来源:igfitidea点击:

Selenium WebDriver: Help getting specific row data from a table

javahtmlselenium-webdriverautomation

提问by Doctor Who

I have a table with 12 columns, one of the columns is named "quantity" what I need to do is find out which column is the quantitiy one and then get all of the data from the table for that column.

我有一个包含 12 列的表,其中一列名为“数量”,我需要做的是找出哪一列是数量一列,然后从表中获取该列的所有数据。

This is some rough code that I have written but at the moment it is just giving me all the data from the table instead of the part that I need.

这是我写的一些粗略的代码,但目前它只是给我表中的所有数据,而不是我需要的部分。

// Get the table name
WebElement table = driver.findElement(By.className("compact"));

        // Get all the Table headers and look for one the element called quantity.
        List<WebElement> allHeaders = table.findElements(By.tagName("th"));
        System.out.println("This is the total numbers of headers" + allHeaders.size());
        int quantityHead = 0;

        // Get the column where the quantity is listed
        for(WebElement header : allHeaders)
        {
            quantityHead++;
            if(header.getText().equalsIgnoreCase("Quantity"))
            {
                System.out.println(header.getText());
                System.out.println(quantityHead + " is the column number for the quantity");
            }
        }

        // Now get all the TR elements from the table
        List<WebElement> allRows = table.findElements(By.tagName("tr"));
        System.out.println("This is how many allRows: " + allRows.size());

        // This is how many items are in the order item table. number of rows - 1 coz top row is the headings
        int z = allRows.size() - 1;
        System.out.println("This is how many items are in the table : " + z );

        // Work out the cells we need to get by
        //  iterate over all the td
        for (WebElement row : allRows) {

            // now we have all the td
            List<WebElement> cells = row.findElements(By.tagName("td"));

            for (WebElement cell : cells) {

             for(int x=0; x <= allHeaders.size(); x++)
             {
                 int y = x;

                 // if y = the value where the qty header is then output the text of that cell
                 if(y == quantityHead )
                 {
                     System.out.println();
                     System.out.println("The quanity is: " + cell.getText());
                 }
             }
         }
        }

回答by bhasker

First identify the column position of the quantity in the table. then just find the element by the xpath

首先确定表中数量的列位置。然后只需通过 xpath 找到元素

WebElement table = driver.findElement(By.className("compact"));

List<WebElement> th=table.findElements(By.tagName("th"));
    int col_position=0;
    for(int i=0;i<th.size();i++){
        if("quantity".equalsIgnoreCase(th.get(i).getText())){
            col_position=i+1;
            break;
        }
    } 
List<WebElement> FirstColumns = table.findElements(By.xpath("//tr/td["+col_position+"]"));
    for(WebElement e: FirstColumns){
        System.out.println(e.getText());
    }    

Hope this helps

希望这可以帮助