Java selenium-按标题选择列

Java selenium-按标题选择列,java,selenium,indexing,html-table,Java,Selenium,Indexing,Html Table,在我的脚本(selenium和Java)中,我需要根据表的标题选择一个表列。 基本上,我将头作为参数发送,然后直接获取该列 但是,问题是我无法获取标题列的索引。 有什么想法或建议吗 <table class="table table-hover tablesorter ib-table" data-reactid=".0.0.0.1.2.0"> <thead data-reactid=".0.0.0.1.2.0.0"> <tr data-reactid=".0.0

在我的脚本(selenium和Java)中,我需要根据表的标题选择一个表列。 基本上,我将头作为参数发送,然后直接获取该列

但是,问题是我无法获取标题列的索引。 有什么想法或建议吗

 <table class="table table-hover tablesorter ib-table" data-reactid=".0.0.0.1.2.0">
<thead data-reactid=".0.0.0.1.2.0.0">
<tr data-reactid=".0.0.0.1.2.0.0.0">
<th data-reactid=".0.0.0.1.2.0.0.0.0">ID</th>
<th data-reactid=".0.0.0.1.2.0.0.0.1">To</th>
<th data-reactid=".0.0.0.1.2.0.0.0.2">From</th>
<th data-reactid=".0.0.0.1.2.0.0.0.3">Text</th>
</tr>
</thead>
<tbody data-reactid=".0.0.0.1.2.0.1" style="height: auto;">
<tr data-reactid=".0.0.0.1.2.0.1.$0">
<td data-reactid=".0.0.0.1.2.0.1.$0.0">123456</td>
<td data-reactid=".0.0.0.1.2.0.1.$0.1">+0156477889785</td>
<td data-reactid=".0.0.0.1.2.0.1.$0.2">+0156477889784</td>
<td data-reactid=".0.0.0.1.2.0.1.$0.3">sample textM</td>
</tr>
<tr data-reactid=".0.0.0.1.2.0.1.$1">
<tr data-reactid=".0.0.0.1.2.0.1.$2">

身份证件
到
从…起
正文
123456
+0156477889785
+0156477889784
示例文本

首先,您需要创建一个包含所有标题的列表

List<WebElement> headersList = driver.findElement(By.xpath("//table[@class='table-hover tablesorter ib-table']")).findElements(By.tagName("th"));
List headersList=driver.findElement(按.xpath(“//table[@class='table-hover tablesorter ib table'])).findElements(按.tagName(“th”);
在那之后,你将把所有的tr都放入循环中

List<WebElement> trList = driver.findElement(By.xpath("//table[@class='table-hover tablesorter ib-table']")).findElements(By.tagName("tr"));

List<WebElement> tdList;

for(int = i ; i < trList.Count ; i++)
{
tdList = trList[i].findElements(By.tagName("td"));

//tdList[0] its header in index[0]... and so on...
}
List trList=driver.findElement(按.xpath(“//table[@class='table-hover tablesorter ib table'])).findElements(按.tagName(“tr”);
列表tdList;
for(int=i;i
这在奇数情况下不起作用,例如使用colspan时。它应该适用于任何“正常”数据表。修剪是为了避免出现在许多Web元素周围不可避免的空白

public static List<WebElement> getColumnByHeaderText(WebDriver driver, By by, String header) {
    WebElement table = driver.findElement(by);
    Function<WebElement, String> elementToString = (WebElement w) -> w.getText().trim();
    List<WebElement> list = table.findElements(By.tagName("th"));
    int index = Lists.transform(list, elementToString).indexOf(header);
    if (index == -1) {
        throw new RuntimeException("Unable to locate header");
    } else {
        return table.findElements(By.cssSelector("td:nth-child(" + (index + 1 ) + ")"));
    }
}
public静态列表getColumnByHeaderText(WebDriver驱动程序、By、字符串头){
WebElement表=driver.findElement(按);
Function elementToString=(WebElement w)->w.getText().trim();
列表=table.findElements(按.tagName(“th”));
int index=Lists.transform(list,elementToString).indexOf(header);
如果(索引==-1){
抛出新的RuntimeException(“无法定位头”);
}否则{
返回表.findElements(By.cssSelector(“td:n子项(“+(索引+1)+”));
}
}

如果您为您尝试过的相同和
java
代码提供
html
代码示例,将更容易为您提供帮助,但是如果没有它,就无法获得索引?我是说。没有属性。index=…您可以通过如下路径访问它:driver.findElement(by.xpath(“//table[@class='table-hover tablesorter ib table']//th[1]”);如果有帮助的话