Java 獲取表格中某一個單元格的值


需求

搜索頁面返回表格樣搜索結果, 獲取搜索結果中某個單元格的具體值.

 

以下圖為例, 下表是搜索返回的結果, 第一行是各個列的名字, 其它是具體的返回值.

 

方法1:

 根據用戶輸入的表頭名來確定是第幾列, 再根據用戶輸入的行數鎖定單元格.

package com.thanos.ebony2;
import static com.thanos.ebony2.bean.AntFormBrowser.BROWSER;
import com.thanos.ebony2.exception.NoSuchRow;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.thanos.ebony2.bean.AntProp;

@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class Test {
    public static void main(String[] args) {  //測試代碼
        BROWSER.open(AntProp.getProp("XXXURL"));
        BROWSER.pause(1);
        BROWSER.click("//*[@id=\"abc\"]/div/div/form/div[2]/div/div/div/span/div[1]/button");
        BROWSER.pause(1);
        System.out.println(getRowNo());
        System.out.println(getColNo());
        String a = getCell("A", 10); // 取A列第10行的值
        System.out.println("cellValue a: " + a);  
        String b = getCell("G", 11); // 取G列第11行的值
        System.out.println("cellValue b: " + b);
    }
    
    
    
    /*
     * 參數: 列名 String tabName; 行數 int rowNo
     * 返回值: 某一個格子的值
     */

    public static String getCell(String colName, int rowNo) {
        int totalRow = getRowNo();
        if (rowNo < 1) {
            throw new NoSuchRow(String.format("row [%s]", rowNo));
        } else if(rowNo > totalRow) {
            throw new NoSuchRow(String.format("There's %s rows, your input %s is not reachable", totalRow, rowNo));
        } 
        
        List<WebElement> temp = BROWSER.getWebDriver().findElements(By.tagName("tr"));
        List<WebElement> currentRow = temp.subList(1, temp.size()-1);  //去掉表頭那一行
        String[] rowData = currentRow.get(rowNo-1).getText().split(" ");  //注: 因第一行是表頭(列名), 所以取單元格值時表頭不計入rowNo. 即: rowNo傳入1時所取的值是表頭下面第一行的值.
        List<WebElement> tags = BROWSER.getWebDriver().findElements(By.tagName("th"));
        List<String> tagNames = new ArrayList();
        for (WebElement ele : tags) {
            String name = ele.getText();
            tagNames.add(name);
        }
//      Float cellValue = 0f;
        String cellValue = "";
        int index = tagNames.indexOf(colName); 
//      cellValue = Float.parseFloat(rowData[index]);
        cellValue = rowData[index];
        return cellValue;
        
    }
    
    public static int getColNo() {
        List<WebElement> elements = BROWSER.getWebDriver().findElements(By.tagName("th"));
//        for (WebElement webElement : elements) {
//            System.out.println(webElement.getText());
//        }
        return elements.size();
    }
    
    public static int getRowNo() {
        List<WebElement> elements = BROWSER.getWebDriver().findElements(By.tagName("tr"));
//        for (WebElement webElement : elements) {
//            System.out.println(webElement.getText());
//        }
        return elements.size()-1;
    }

}
表頭名 + 行數

 

 

方法2:

用兩層循環把每一個單元格放入二維數組, 再根據用戶輸入的行數和列數定位單元格並取值.

package com.thanos.ebony2;
import static com.thanos.ebony2.bean.AntFormBrowser.BROWSER;
import com.thanos.ebony2.exception.NoSuchRow;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.thanos.ebony2.bean.AntProp;

@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class Test {
    public static void main(String[] args) {  //測試代碼
        BROWSER.open(AntProp.getProp("XXXURL"));
        BROWSER.pause(1);
        BROWSER.click("//*[@id=\"abc\"]/div/div/form/div[2]/div/div/div/span/div[1]/button");
        BROWSER.pause(1);
        System.out.println(getRowNo());
        System.out.println(getColNo());
        String b = getCellValue(10, 1);  //取第10行第1列的值
        System.out.println("cellValue b: " + b);
    }
    
    
    
    /*
     * 參數: 列 int colNo, 行 int rowNo
     * 返回值: 某一個格子的值
     */

    public static String getCellValue(int colNo, int rowNo) {
        List<WebElement> tempR = BROWSER.getWebDriver().findElements(By.tagName("tr"));
        List<WebElement> tempRow = tempR.subList(1, tempR.size()-1);  //去掉表頭(表頭不計入行數)
        int totalRow = getRowNo();
        int totalCol = getColNo();
        Object[][] table = new Object[totalRow][totalCol];
        for (int i = 0; i < tempRow.size(); i++) {
            List<WebElement> currCol = tempRow.get(i).findElements(By.tagName("td"));
            for (int j = 0; j < currCol.size(); j++) {
                table[i][j] = currCol.get(j).getText();
            }
        }
        
        return (String) table[colNo-1][rowNo-1];  //返回單元格的值
    }
    
    
    
    public static int getColNo() {
        List<WebElement> elements = BROWSER.getWebDriver().findElements(By.tagName("th"));
//        for (WebElement webElement : elements) {
//            System.out.println(webElement.getText());
//        }
        return elements.size();
    }
    
    public static int getRowNo() {
        List<WebElement> elements = BROWSER.getWebDriver().findElements(By.tagName("tr"));
//        for (WebElement webElement : elements) {
//            System.out.println(webElement.getText());
//        }
        return elements.size()-1;
    }

}
行數 + 列數

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM