最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - selenium webdriver to get visible row number in table - Stack Overflow

programmeradmin0浏览0评论

I am writing test script using selenium webdriver and java for page which has a table with multiple rows column. 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. 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. This I have achieved by using for loop but it is too time consuming. 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? or can we get the javascript which run using selenium and return the number of row which are displayed on page.

I am writing test script using selenium webdriver and java for page which has a table with multiple rows column. 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. 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. This I have achieved by using for loop but it is too time consuming. 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? or can we get the javascript which run using selenium and return the number of row which are displayed on page.

Share Improve this question edited Jul 26, 2015 at 8:48 nathanchere 8,09815 gold badges68 silver badges86 bronze badges asked Dec 21, 2014 at 11:07 Karim NarsindaniKarim Narsindani 4544 gold badges14 silver badges39 bronze badges 2
  • How rows are hidden? Using style="display:none;" in tr? – Ruslan Ostafiichuk Commented Dec 21, 2014 at 11:59
  • Yes Ruslan, it has same style as you mention (class="row" style="display: none;") in tr – Karim Narsindani Commented Dec 21, 2014 at 12:06
Add a ment  | 

2 Answers 2

Reset to default 3

You can filter invisible tr using proper xpath, like:

//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);
发布评论

评论列表(0)

  1. 暂无评论