WebDriver獲取table的內容(通過動態獲取Table單元格的TagName對其innerHTML值進行獲取)


import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Table {
    // 通過JS動態獲取到的Html的TagName
    String tagname = "";
    //
    String text = "";
    // 用於存放Map的key值
    List<String> Key = new ArrayList<String>();
    // 用於存放Map的value值
    List<String> Value = new ArrayList<String>();
    // 將在頁面獲取的table里面的數據以鍵值對的方式存放到map中
    Map<String, String> LinkMap = new LinkedHashMap<String, String>();

    // selector:css選擇器 :定位table下的所有tr標簽
    public void tableValue(WebDriver driver, By selector) {

        JavascriptExecutor JS = (JavascriptExecutor) driver;
        // 首先得到表格中所有tr標簽的集合
        List<WebElement> rows = driver.findElements(selector);

        for (WebElement row : rows) {

            // 然后得到當前所有tr里td標簽的集合
            List<WebElement> cols = row.findElements(By.tagName("td"));
            for (WebElement col : cols) {
                if (col.isDisplayed()) {// 防止得到最下方的隱藏的td單元格(多余的一部分,應為設計失誤)
                    // 如果executeScript()方法中執行的結果有返回值,則需要將其返回,如果僅僅是action或者改變屬性值,則不需要返回,此處為了返回td下的子節點的標簽名,用於后續的判斷
                    tagname = (String) JS.executeScript(
                            "return arguments[0].children[0]?arguments[0].children[0].tagName:arguments[0].tagName;",
                            col);
                    if (tagname.equals("SPAN")) {
                        // 使用正則表達式,處理掉不需要的字符"*"與":"
                        text = col.getText().replaceAll("[*:]", "");

                        Key.add(text);
                    } else if (tagname.equals("INPUT")) {
                        text = col.findElement(By.tagName("input")).getAttribute("value");
                        Value.add(text);
                    } else if (tagname.equals("TD")) {
                        // 使用正則表達式,處理掉不需要的字符"*"與":"
                        text = col.getText().replaceAll("[*:]", "");

                        Key.add(text);
                    } else if (tagname.equals("DIV")) {
                        text = col.findElement(By.tagName("input")).getAttribute("value");
                        Value.add(text);
                    } else if (tagname.equals("SELECT")) {
                        // 獲取當前select下拉框被選中的文本的index值,僅僅只是index值,並不是其innerHTML值
                        text = JS.executeScript("return arguments[0].children[0].selectedIndex;", col).toString();
                        int index = Integer.parseInt(text);
                        // 通過被選中的index獲取其innerHTML值
                        text = (String) JS.executeScript(
                                "return arguments[0].children[0].children[" + index + "].innerHTML;", col);
                        Value.add(text);
                    } else {
                        return;
                    }

                }

            }
        }
        // 將key和value值存入map中,並做了對應的關聯關系
        for (int i = 0; i < Value.size(); i++) {

            LinkMap.put(Key.get(i), Value.get(i));
        }
        Iterator it = LinkMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entity = (Entry) it.next();
            System.out.println("key=" + entity.getKey() + ",value=" + entity.getValue());
        }
    }
}


免責聲明!

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



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