尝试使用 Selenium WebDriver 从 HTML table 获取数据 Java

Trying to get data from HTML table using Selenium WebDriver for Java

我是 Selenium 的新手,我一直在尝试让测试套件从 table 收集数据。我对如何执行此操作一无所知。

这是我正在使用的table: http://i.imgur.com/vdITVug.jpg

新约会(日期)在一天中的随机时间随机添加。我已经创建了一个测试套件,它将不断刷新此页面。下一步是将所有日期保存在 table 中,创建一个循环来比较刷新后的日期是否与原始存储的日期不同。

如果不同,通知用户。

这是我正在努力完成的理论示例。

//Navigate to the appointment page

//Store all the current dates from the table

  for (until a new appointment pops up)

    {
     //Refresh the page
    // Compare the dates to the stored dates
       if (the dates =/ stored dates)
         {
          notify the user(me in this case)
         }
    }

我也在想办法找到 table 的元素 ID。

这是一些 html 代码的屏幕截图:http://i.imgur.com/GD4yOp9.png

突出显示的语句存储了第一个日期。

任何建议将不胜感激,谢谢!

尝试复制一个类似的 HTML 结构(实际上有 2 个,一个在刷新之后)。这是一个快速解决方案,供您比较刷新后的 HTML table。

此处的关键是将您的 table 数据组织成 Map<String, List<String>> 类数据结构。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CheckTables {

public WebDriver driver;


public static void main(String[] args) throws Exception {


    CheckTables objTest = new CheckTables();
    objTest.runTest();

}

public void runTest(){

    driver = new FirefoxDriver();

    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_1.html");
    Map<String, List<String>> objTable_1 = readTable();
    System.out.println("TABLE:1" + objTable_1);

    //event to refresh the table
    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_2.html");
    Map<String, List<String>> objTable_2 = readTable();
    System.out.println("TABLE:2" + objTable_2);

    compareTables(objTable_1, objTable_2);

}

public Map<String, List<String>> readTable(){

    Map<String, List<String>> objTable = new HashMap<>();

    List<WebElement> objRows = driver.findElements(By.cssSelector("tr#data"));
    for(int iCount=0; iCount<objRows.size(); iCount++){
        List<WebElement> objCol = objRows.get(iCount).findElements(By.cssSelector("td.tableTxt"));
        List<String> columns = new ArrayList<>();
        for(int col=0; col<objCol.size(); col++){
            columns.add(objCol.get(col).getText());
        }
        objTable.put(String.valueOf(iCount), columns);
    }

    return objTable;
}

public void compareTables(Map<String, List<String>> objTable1, Map<String, List<String>> objTable2){


    for(int count=0; count<objTable1.size(); count++){

        List<String> objList1 = objTable1.get(String.valueOf(count));
        System.out.println(objList1);
        List<String> objList2 = objTable2.get(String.valueOf(count));
        System.out.println(objList2);

        if(objList1.containsAll(objList2)){
            System.out.println("Row [" + count + "] is SAME");
        }
        else{
            //notify
            System.out.println("Row [" + count + "] has CHANGED");
        }
    }
}
}

这是 RoadTest_1.html 和 RoadTest_2.html 的 HTML 片段 -- https://gist.github.com/anonymous/43c3b1f44817c69bd03d/