尝试使用Selenium WebDriver for Java从HTML表中获取数据 [英] Trying to get data from HTML table using Selenium WebDriver for Java
问题描述
我对Selenium还是很陌生,我一直在尝试使测试套件从表中收集数据.我对如何做到这一点一无所知.
I'm very new to Selenium and I've been trying to make the test suite gather data from a table. I don't have the slightest clue on how to do this.
这是我正在使用的桌子: http://i.imgur.com/vdITVug.jpg
Here's the table I am working with: http://i.imgur.com/vdITVug.jpg
每天的随机时间会随机添加新约会(日期).我创建了一个测试套件,该套件将在此页面上不断刷新.下一步是将所有日期保存在表中,创建一个循环以比较刷新后的日期是否与原始存储的日期不同.
New appointments (dates) are randomly added at random times of the day. I've created a test suite that will constantly refresh at this page. The next step, would be to save all the dates in the table, create a loop to compare if the dates after a refresh happen to be different the original stored dates.
如果它们不同,请通知用户.
If they are different, notify the user.
这是我要完成的工作的理论示例.
Here's a theoretical example of what I'm trying to accomplish.
//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)
}
}
我还试图弄清楚如何找到表的元素ID.
I'm also trying to figure out how I can find the element ID of the table.
以下是一些html代码的屏幕截图: http://i.imgur.com/GD4yOp9. png
Here's a screenshot with some of the html code: http://i.imgur.com/GD4yOp9.png
突出显示的语句存储了第一个日期.
The statement that is highlighted has the first date stored.
任何建议将不胜感激,谢谢!
Any advice would be appreciated, thanks!
推荐答案
试图复制类似的HTML结构(实际上是其中的2种,刷新后一种).这是一个快速的解决方案,供您在刷新后比较HTML表.
Tried replicating a similar HTML structure (in fact 2 of them, one after the refresh). Here is a quick solution for you to compare the HTML tables after refresh.
这里的关键是将表数据组织成类似数据结构的Map<String, List<String>>
.
The key here is organizing your table data into a Map<String, List<String>>
like data structure.
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/
Here are the HTML snippets for RoadTest_1.html and RoadTest_2.html -- https://gist.github.com/anonymous/43c3b1f44817c69bd03d/
这篇关于尝试使用Selenium WebDriver for Java从HTML表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!