gpt4 book ai didi

java - 在表中搜索给定文本,如果存在则应显示通过

转载 作者:行者123 更新时间:2023-11-30 02:38:52 25 4
gpt4 key购买 nike

我的问题是,我有一个动态表,我想搜索表中是否存在给定的字符串。如果存在则通过,否则失败。我的代码是

Boolean isPresent = false;  
WebElement mytable = driver.findElement(By.xpath("html/body/div[1]/div[1]/form/div/table")); //To locate rows of table.
List < WebElement > rows_table = mytable.findElements(By.tagName("tr")); //To calculate no of rows In table.
int rows_count = rows_table.size(); //Loop will execute for all the rows of the 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 the cells.
celltext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number " + row + " and column num ber " + column + " Is " + celltext);
if (celltext.equals("dfsahdfakjfhka")) {
System.out.println("passed on: " + celltext);
isPresent=true;
}
else {
System.out.println("failed on: " + celltext);
isPresent=false;
}
}
}
if (isPresent==true) {
Assert.assertFalse(true);
}
else{
Assert.assertFalse(false);
}

当它运行时。没有显示错误。实际上我的表没有值“”dfsahdfakjfhka”。通过和失败消息显示正确,并且 boolean 值“isPresent”返回 false。但是断言在这里不起作用

最佳答案

您不需要循环遍历所有 tr 和 td,您只需获取整个表格的文本,然后查看您想要的文本是否在其中。

@Test
public void verifyTableContainsText(){
WebDriver driver = new FirefoxDriver();
driver.get("https://www.w3schools.com/html/html_tables.asp");
WebElement table = driver.findElement(By.id("customers"));
Assert.assertTrue(isTableContainsText(table, "Germany"));
Assert.assertFalse(isTableContainsText(table, "India"));

}

public boolean isTableContainsText(WebElement table, String text){
if (table.getText().contains(text)){
return true;
}

return false;
}

关于java - 在表中搜索给定文本,如果存在则应显示通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42338780/

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