[英]How to write a generic method findTableRowBy() in Selenium Java?
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
As you can see from below simple table, find table column with text equals "January" is simple: "tr/td[text()='January']" But the td may get complex as it may contains nested elements. 从下面的简单表中可以看出,查找文本等于“ January”的表列很简单:“ tr / td [text()='January']”但是td可能会变得复杂,因为它可能包含嵌套元素。 So we'd better provide another By parameter.
因此,我们最好提供另一个By参数。
My question is how to write a generic method that can perform this search. 我的问题是如何编写可以执行此搜索的通用方法。 I have one below, but not good, findTableRowsBy( 1, By.xpath( "text()='January'" ) ) will lead to exception.
我有一个在下面,但并不好,findTableRowsBy(1 By.xpath( “文本()= '月'”))会导致异常。 Anybody could help me polish this method?
有人可以帮助我完善此方法吗? Thanks very much.
非常感谢。
public List<WebElement> findTableRowsBy( int column, By theBy )
{
By by = By.xpath( "tbody[1]/tr/td[" + column + "]" );
List<WebElement> cols = table.findElements( by );
List<WebElement> rows = new ArrayList<WebElement>();
for( WebElement e : cols )
{
List<WebElement> eles = e.findElements( theBy );
if( eles.isEmpty() )
continue;
if( eles.size() == 1 )
rows.add( eles.get( 0 ) );
}
return rows;
}
Maybe, you could also use the following xpath: 也许,您也可以使用以下xpath:
//td[contains(text(), "January")]
I guess you should be able to find the proper td even if it has nested elements. 我想即使它有嵌套元素,您也应该能够找到合适的td。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.