gpt4 book ai didi

java - 如何获取由 Selenium 中特定列值选择的单元格

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:35 25 4
gpt4 key购买 nike

我需要为动态表选择订单 ID #1968 的单元格。

我已经使用下面的代码打印了表格,请建议我如何获取选定值#1968的特定单元格?

	WebElement Table_element =commonFunctions.driver.findElement(By.xpath("//*[@id='active']/div/table"));
//To locate rows of table.
List < WebElement > rows_table = Table_element.findElements(By.tagName("tr"));
//To calculate no of rows In table.
int rows_count = rows_table.size();
//Loop will execute till the last row of table.
for (int row = 0; row < rows_count; row++) {
//To locate columns(cells) of that specific row.
List < WebElement > Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns (cells). In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are " + columns_count);
//Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
celtext = Columns_row.get(column).getText();
System.out.println("Cell Value of row number " + row + " and column number " + column + " Is " + celtext);


}
}

Result of table : Number of cells In Row 1 are 4 Cell Value of row number 1 and column number 0 Is #1975 Cell Value of row number 1 and column number 1 Is SERVICE1535721248947 FOR $156 - Edited service Name Cell Value of row number 1 and column number 2 Is Delivered Cell Value of row number 1 and column number 3 Is Nov 5 Number of cells In Row 2 are 4 Cell Value of row number 2 and column number 0 Is #1971 Cell Value of row number 2 and column number 1 Is service1538918641775 Cell Value of row number 2 and column number 2 Is Delivered Cell Value of row number 2 and column number 3 Is Oct 18 Number of cells In Row 3 are 4 Cell Value of row number 3 and column number 0 Is #1969 Cell Value of row number 3 and column number 1 Is service1538918641775 Cell Value of row number 3 and column number 2 Is Delivered Cell Value of row number 3 and column number 3 Is Oct 18 Number of cells In Row 4 are 4 Cell Value of row number 4 and column number 0 Is #1968 Cell Value of row number 4 and column number 1 Is service1538918641775 Cell Value of row number 4 and column number 2 Is Delivered Cell Value of row number 4 and column number 3 Is Oct 18

表格的HTML:

  <table class="table table-bordered table-hover">
<thead>
<tr>
<th>Order#</th>
<th>Title</th>
<th>Status</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<tr class="entry">
<td class="gig first">
<div><a href="#">
#1983
</a>
</td>
<td><a href="#">SERVICE1535721248947 FOR $156 - Edited service Name</a></td>
<td class="status delivered" ><span class="label mt1 order-label-delivered">Delivered</span></td>
<td class="datetime last"><div>Nov 11</div></td>
</tr>
<tr class="entry">
<td class="gig first">
<div><a href="#">
#1982
</a>
</td>
<td><a href="#">SERVICE1535721248947 FOR $156 - Edited service Name</a></td>
<td class="status delivered"><span class="label mt1 order-label-delivered">Delivered</span></td>
<td class="datetime last"><div>Nov 11</div></td>
</tr>

</tbody>
</table>

最佳答案

我不知道我是否理解正确,但您想遍历订单 ID 表并找到特定的一个。

我会怎么做:

List<WebElement> orders = this.getDriver().findElements(By.xpath("/html/body/table/tbody/tr/td[1]/div/a"));

该代码将检索第一列内的所有元素并将其添加到订单列表中。

完成此操作后,您可以拥有一个整数索引变量,该变量将跟踪特定元素的行号(如果存在)。

int index = 0; 

for(WebElement elements: orders){
index++;
if(elements.text.equals("#1968"){
//Here you can click on the link or do whatever...
this.getDriver().findElement(By.xpath("/html/body/table/tbody/tr[index]/td[1]/div/a")).click();
}

}

我不记得 selenium 的确切语法,但我知道我迭代了一个元素列表,并根据我的数据集单击了特定的一个。

请注意,有时 selenium 驱动程序会因找不到元素而返回错误,请使用

WebDriverWait wait = new WebDriverWait(this.getDriver(), 30)

延长持续时间。

另一个有用的东西是 Action 类,所以这里是更好的解决方案:

Action act = new Action(this.getDriver());
act.click(wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))).build().perform();

关于java - 如何获取由 Selenium 中特定列值选择的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com