我被要求创建一个自动化工具,以使用我所工作公司的内部网页中的数据来创建Excel报表。我选择python(3.7)作为我的语言(因为我的大多数同事也使用它)和Selenium来完成这项工作。单击并使用Selenium填充所有字段以过滤所需的数据后,我至少得到了60页,每页20行(数量可能有所不同),因此我必须将每页数据复制到excel工作表中,然后单击单击“下一步”按钮,再次复制数据,依此类推,直到处理完所有页面。我能够复制网站第一页的数据,但是当我尝试执行“下一个”过程时,它无法将数据保存在excel工作表中。这是我正在使用的代码
***Bunch of Selenium steps***
def excelFactory(values,row,col): #fuction to create the excel workbook
fname = 'App_Report.xlsx'
if os.path.exists(fname): #check if the workbook is already created
wb = openpyxl.load_workbook(fname)
ws = wb.get_sheet_by_name('Sheet')
else: #create the workbook if not found
wb = Workbook()
ws = wb.active
ws.cell(row= row,column= col).value = values #insert the data from the online web in the
#workbook
row = row + 1
wb.save(fname)
def dataDownload(): #function to parse thru the online table and read the data
data = driver.find_elements_by_class_name("rowNumber")
for td in data:
values = td.text
print(td)
excelFactory(values,col)
nxtButton = driver.find_elements_by_class_name('nxtButtonClassname')
for content in nxtButton:
if content.text == 'Next':
content.click()
dataDownload()
dataDownload()