简体   繁体   English

硒webdriver获取表中的可见行号

[英]selenium webdriver to get visible row number in table

I am writing test script using selenium webdriver and java for page which has a table with multiple rows column. 我正在使用Selenium Webdriver和Java编写具有页面多行列表的测试脚本。 This rows has same style and class. 此行具有相同的样式和类。 However, some of rows are hidden and some of them are displayed on page. 但是,有些行是隐藏的,而有些则显示在页面上。 There are ~1800 rows from which only seven rows are displayed on page. 大约有1800行,页面上仅显示7行。 Now, I have to work on visible row for process data, I have created xpath but it is taking very long time to verify which row number is visible on page and if visible than proceed further with running script else check next row. 现在,我必须处理过程数据的可见行,已经创建了xpath,但是要花很长时间才能验证页面上可见的行号,如果可见,则继续运行脚本,否则检查下一行。 This I have achieved by using for loop but it is too time consuming. 我已经通过使用for循环实现了,但是这太浪费时间了。 Well there is a drop down option in rows but the id of those drop down is dynamic based on table row like testid_0 - row 1, testid_1 - row 2. My question is there any way we can get the visible row number of table like row 7, row 100, row 500 which is visible on page without spending time or using for loop? 好吧,行中有一个下拉选项,但是这些下拉列表的ID是基于表行动态生成的,例如testid_0-行1,testid_1-行2。 7,第100行,第500行在页面上可见而无需花费时间或使用for循环? or can we get the javascript which run using selenium and return the number of row which are displayed on page. 或者我们可以获取使用selenium运行的javascript并返回页面上显示的行数。

您可以使用适当的xpath过滤不可见的tr ,例如:

//table/tbody/tr[not(contains(@style,'display: none;'))]

You can also try the below code to get the count of rows that are visible( Assuming there is just one table in the webpage ): 您还可以尝试以下代码来获取可见行的计数( 假设网页中只有一个表 ):

int count = 0;
List<WebElement> rows = driver.findElements(By.xpath("//table//tr"));
for(WebElement row: rows){
  if(row.isDisplayed())
    count++;
}
System.out.println("The number of rows that are visible is: "+ count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM