enter image description here我目前正在尝试遍历包含某些信息的数据网格。我能够遍历实际的表头并获取列名,但这将用作识别列数据的基础。
我正面临以下挑战, 1.我能够提取网格上的每一行并添加到数组列表中。 2.对于每一行,我都可以将每个单元格条目广告到另一个数组列表,但是可以基于正则表达式空间进行拆分。 3.因为一个单元格值可以有两个字符串值,所以每个值都分别添加到课程数组中,但这不是我想要的。
我想使用列标题作为索引来查找特定行条目的单元格值。
示例列标题3的索引为2,我想使用该索引来获取单元格3的完整单元格值。
我的代码:
List<String> allocationValues, separatedValue, temp;
String cellText = null;
List<WebElement> rows = mydriver.findElements(By.cssSelector("tbody > tr"));
List<WebElement> headersCSS = mydriver.findElements(By.xpath("//*[@id=\"datagrid_2003_head\"]/thead/tr/th"));
int key = 0;
HashMap<Integer, String> headerHM = new HashMap<>();
//Getting column headers, this will suffice for now
for(WebElement header : headersCSS){
headerHM.put(key,header.getText());
key += 1;
}
for (WebElement row : rows) {
if (row.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr[4]/td/div/div[2]/div[2]/div/div/table/tbody/tr[2]/td/div/div[2]/div[1]/div/div[1]/div[2]/div[3]/table/tbody")).getText().equals("Product One")) ;
cellText = mydriver.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr[4]/td/div/div[2]/div[2]/div/div/table/tbody/tr[2]/td/div/div[2]/div[1]/div/div[1]/div[2]/div[3]/table/tbody")).getText();
break;
}
allocationValues = Arrays.asList(cellText.split("\n"));
for (String entry: allocationValues) {
separatedValue = Arrays.asList(entry.split(" "));
}
//clear and close the hashMap
headerHM.clear();
break;