如何使用 selenium python webdriver 从 Web 应用程序计算表中的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14831041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:34:50  来源:igfitidea点击:

How to count no of rows in table from web application using selenium python webdriver

pythonselenium-webdriver

提问by Kv.senthilkumar

How to count the rows in the table from web application by using selenium python web driver. Here we can retrieve all data in the table from web application but couldn't count the rows and columns, please give me idea of how to do this.

如何使用 selenium python Web 驱动程序从 Web 应用程序计算表中的行数。在这里,我们可以从 Web 应用程序检索表中的所有数据,但无法计算行和列,请告诉我如何执行此操作。

回答by Santoshsarma

Try some thing like this

尝试这样的事情

int rowCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr")).size();

int columnCount=driver.findElements(By.xpath("//table[@id='DataTable']/tbody/tr/td")).size();

FYI : This is the implementation in java.

仅供参考:这是java中的实现。

回答by Hans Wouters

Selenium has a function for counting the XPath result, see http://release.seleniumhq.org/selenium-core/1.0/reference.html. According to this you can use storeXpathCount.

Selenium 具有计算 XPath 结果的功能,请参阅http://release.seleniumhq.org/selenium-core/1.0/reference.html。据此,您可以使用 storeXpathCount。

I am using the DefaultSelenium class from the selenium-java-client-driver, where I can use (also in java) the following:

我正在使用 selenium-java-client-driver 中的 DefaultSelenium 类,我可以在其中使用(也在 java 中)以下内容:

int rowCount = selenium.getXpathCount("//table[@id='Datatable']/tbody/tr").intValue()

回答by Adam Wildavsky

Santoshsarma's answer is close to correct for Java, but it will count the number of cells rather than the number of columns. Here's a Python version that will count the number of columns in the first row:

Santoshsarma 的答案对于 Java 接近正确,但它会计算单元格数而不是列数。这是一个 Python 版本,它将计算第一行中的列数:

row_count = len(driver.find_elements_by_xpath("//table[@id='DataTable']/tbody/tr"))
column_count = len(driver.find_elements_by_xpath("//table[@id='DataTable']/tbody/tr/td"))

If your table has a header you could count the columns there instead.

如果您的表格有标题,您可以计算那里的列数。

I haven't tried using storeXpathCount.

我还没有尝试过使用 storeXpathCount。

回答by Andriy Ivaneyko

You can do it by using find_elements_byor execute_script.

您可以使用find_elements_byexecute_script 来完成

Retrieving count by applying lenon result of find_elements_byis correct way, however in other turn getting count of rows and columns by using execute_scriptis quite fasterthen getting lenfrom result of find_elements_bymethod call:

通过应用检索计数len个对结果find_elements_by是正确的做法,但在其他依次通过使用获得的行和列数execute_script相当快然后让len从结果find_elements_by的方法调用:

rows_count = driver.execute_script("return document.getElementsByTagName('tr').length")
columns_count = driver.execute_script("return document.getElementsByTagName('tr')[0].getElementsByTagName('th').length")

Below is performance metrics of using find_elements_by vs execute_scriptapproach:

以下是使用find_elements_by 与 execute_script方法的性能指标:

from timeit import timeit

# Time metrics of retrieving 111 table rows
timeit(lambda:driver.execute_script("return document.getElementsByTagName('tr').length"), number=1000)
# 5.525
timeit(lambda:len(driver.find_elements_by_tag_name("tr")), number=1000) 
# 9.799
timeit(lambda:len(driver.find_elements_by_xpath("//table/tbody/tr")), number=1000)
# 10.656

# Time metrics of retrieving 5 table headers
timeit(lambda:driver.execute_script("return document.getElementsByTagName('tr')[0].getElementsByTagName('th').length"), number=1000)
# 5.441
timeit(lambda:len(driver.find_elements_by_tag_name("th")), number=1000)
8.604
timeit(lambda:len(driver.find_elements_by_xpath("//table/tbody/th")), number=1000)
# 9.336

回答by user3036030

For python users the simple way of counting a tag is as follows.

对于 python 用户,计算标签的简单方法如下。

!#use find_elements_by_xpath

test_elem = self.driver.find_elements_by_xpath("//div[@id='host-history']/table[1]/tbody[1]/tr")

!#then check the len of the returnes list

print (len(test_elem))