我试图通过元素在表中找到一行,然后在该行中找到一个元素并返回一个属性(或者做其他的事情)。
这里的java selenium代码找到了一个表体。 getText()准确地打印出每行中行中的文本,但s.findElement(xpath).getText()将返回unique_cell_name,无论该行是否包含它。每个循环都会输入if语句。
我该如何解决?为什么getText()返回正确的文本但我找不到相同rowElement中的元素?
List<WebElement> listOfRows2 = driver().findElement(By.xpath("//table/tbody")).waitUntilPresent().findElements(By.xpath("./tr"));
for(WebElement rowElement : listOfRows2){
System.out.println("getText: " + rowElement.getText());
String name = rowElement.findElement(By.xpath("//a[contains(text(),'unique_cell_name')]")).getText();
System.out.println("name : " + tooltip);
if(name.equals('unique_cell_name')){
rowElement.findElement(By.xpath("//a[contains(text(),'unique_cell_name_to_row_but_not_table')]")).doSomething();
//also want to check text in a specific column 5, where the same text might exist in the same row in the different column
rowElement.findElement(By.xpath("//td[2]")).doSomething();
break;
}
}
我在java中使用selenium 2.53,带有宁静/黄瓜。
答案 0 :(得分:0)
问题是GrassHopper提到的By.xpath(&#34; .// td [2]&#34;)中搜索节点的孩子的时间
List<WebElement> listOfRows2 = driver().findElement(By.xpath("//table/tbody")).waitUntilPresent().findElements(By.xpath("./tr"));
for(WebElement rowElement : listOfRows2){
System.out.println("getText: " + rowElement.getText());
//This if statement is to check whether the row has the element or not, otherwise it will throw an error
if(rowElement.findElements(By.xpath(".//a[contains(text(),'unique_cell_name')]").size() !=0){
String name = rowElement.findElement(By.xpath(".//a[contains(text(),'unique_cell_name')]")).getText();
System.out.println("name : " + name);
if(name.equals('unique_cell_name')){
rowElement.findElement(By.xpath(".//a[contains(text(),'unique_cell_name_to_row_but_not_table')]")).doSomething();
//also want to check text in a specific column 5, where the same text might exist in the same row in the different column
rowElement.findElement(By.xpath(".//td[2]")).doSomething();
break;
}
}
else{
System.out.println("This is not the row you are looking for");
}
}