Python+Selenium实现列表数据的翻页获取
涉及到的定位元素
1、列表表格tbody 2、列表总数据元素
3、分页数据元素 4、下一页图标/按钮元素
def get_list_all_datas(self, table_tbody, total_num_loc, pagination, pagination_selected, next_page, doc=''):
"""
参数说明
:param table_tbody: 列表表格的tbody元素定位
:param total_num_loc: 列表总数据元素定位
:param pagination: 分页框元素定位
:param pagination_selected: 分页数据元素定位(例如:X 页/条)
:param next_page: 下一页图标元素定位
:param doc:
:return:
"""
time.sleep(1)
list_1 = []
element = self.driver.find_element(By.XPATH, table_tbody)
element_total_num = self.get_element_text(total_num_loc, doc="获取列表总数据")
self.click(pagination, doc="点击分页弹框")
time.sleep(0.5)
element_pagination = self.get_element_text(pagination_selected, doc="获取页码分页")
self.click(pagination, doc="点击分页弹框")
num_1 = int(re.findall(r"\d+\.?\d*", element_total_num)[0])
num_2 = int(re.findall(r"\d+\.?\d*", element_pagination)[0])
print("列表页面总数据共{}条".format(num_1))
print("列表分页显示为{}条/页".format(num_2))
if num_1 <= num_2:
ret_page = 1
print("列表页面数据共{}页".format(ret_page))
else:
ret_page = (num_1 // num_2) + 1
print("列表页面数据共{}页".format(ret_page))
i = 1
while i <= ret_page:
table_tr_list = element.find_elements(By.TAG_NAME, "tr")
for tr in table_tr_list:
list_2 = tr.get_attribute('innerText').replace("\n\t\n", ",").replace("\t\n", " ,")
list_1.append(list_2)
time.sleep(1)
self.click(next_page, doc="点击下一页图标")
time.sleep(1)
i += 1
if list_1 != []:
ret_info1 = list_1
ret_info2 = len(list_1)
return ret_info1, ret_info2, num_1
else:
ret_info1 = "列表数据为空,列表数为0"
ret_info2 = 0
return ret_info1, ret_info2, num_1
测试报告显示示例
|