如何在 Selenium 和 Python 中统计页面表格内特定文本出现的次数?
我们可以使用 Selenium 统计表格内特定文本出现的次数。首先,我们需要使用 xpath 定位元素。在 xpath 中,我们有一个特定的 text() 函数,它可以根据屏幕上可见的文本识别元素。
然后,我们需要使用 find_elements 方法获取页面上包含我们查找文本的所有匹配元素的列表。最后,我们需要使用列表的 len 函数获取该列表的大小。
这将给出表格内特定文本出现的次数。
语法
driver.find_elements_by_xpath("//td[text()='Tutorialspoint']")示例
获取表格内文本出现次数的代码实现。
from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/plsql/plsql_basic_syntax.htm")
#to refresh the browser
driver.refresh()
# identifying the text keyword inside the table
dta = driver.find_elements_by_xpath("//td[text()='keyword']")
# len method is used to get the count of occurrences
print(len(dta))
#to close the browser
driver.close()
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP