Java 搜索表中的给定文本,如果存在,则应显示“通过”
我的问题是,我有一个动态表,我想搜索表中是否存在给定的字符串。如果存在,则通过,否则失败。我的代码是Java 搜索表中的给定文本,如果存在,则应显示“通过”,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我的问题是,我有一个动态表,我想搜索表中是否存在给定的字符串。如果存在,则通过,否则失败。我的代码是 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.fin
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);
}
Boolean isPresent=false;
WebElement mytable=driver.findElement(By.xpath(“html/body/div[1]/div[1]/form/div/table”)//查找表的行。
Listrows\u table=mytable.findElements(按.tagName(“tr”))//计算表中的行数。
int rows\u count=rows\u table.size()//循环将对表的所有行执行
对于(int row=0;rowColumns\u row=rows\u table.get(row.findElements)(按.tagName(“td”);
//计算特定行中的列(单元格)数。
int columns_count=columns_row.size();
//System.out.println(“行“+行+”中的单元格数为“+列数”);
//循环将一直执行到该特定行的最后一个单元格。
for(int column=0;column
当它运行时。没有显示错误。实际上,我的表没有值“dfsahdfakjfhka”“.Passed和Failed消息显示正确,布尔值valeu'isPresent'返回false。但是assert在此不起作用您不需要循环遍历所有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;
}
请参阅本节。有什么问题吗?它不起作用。我总是返回false。我给的是xpath而不是id。请suggest@Vishnu你的意思是上面的例子不起作用,或者你为你编写的代码不起作用?谢谢…Gaurang…工作正常…我在代码中犯了一些错误…再次感谢