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());
        }
    }
}

原文地址:https://www.cnblogs.com/zw520ly/p/5901091.html