1 from selenium import webdriver 2 import time 3 import xlwt 4 5 def get_test_result(): 6 ‘‘‘ 7 获取测试报告网页中的结果数据 8 :return: 9 ‘‘‘ 10 driver = webdriver.Chrome() 11 driver.get(r"C:\Users\19641\PycharmProjects\docker_practice\data\多媒体应用自动化测试_20200707165458.html") 12 time.sleep(2) 13 # 获取列名字段(headers) 14 headers = list() 15 titles = driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/table/thead/tr") 16 th_lists = titles.find_elements_by_tag_name("th")[:-1] 17 for th_list in th_lists: 18 title = th_list.text 19 headers.append(title) 20 # 获取表格数据 21 table = driver.find_element_by_id("detailBody") 22 table_sheet = list() 23 tr_lists = table.find_elements_by_tag_name("tr") 24 for tr_list in tr_lists: 25 td_lists = tr_list.find_elements_by_tag_name("td")[:-1] 26 table_row = list() 27 for td_list in td_lists: 28 td_text = td_list.text 29 table_row.append(td_text) 30 table_sheet.append(table_row) 31 # print(table_sheet) 32 driver.quit() 33 # 将headers数据插入到首位 34 table_sheet.insert(0, headers) 35 return table_sheet 36 37 # 将测试报告中的数据写入到excel表格中 38 datas = get_test_result() 39 wb = xlwt.Workbook() 40 sh = wb.add_sheet(‘测试报告数据‘) 41 h = 0 42 for data in datas: 43 l = 0 44 for cell in data: 45 sh.write(h, l, cell) 46 l = l + 1 47 h = h + 1 48 wb.save(r"C:\Users\19641\PycharmProjects\docker_practice\data\测试报告数据.xls")