1 # -*- coding: utf-8 -*- 2 from selenium import webdriver 3 from openpyxl import load_workbook 4 class mylogintest(): 5 ''' Open Baidu Test ''' 6 7 def openBD(self): 8 '' ' Open Baidu ''' 9 print ( " begin " ) 10 self.driver = webdriver.Firefox() 11 book = load_workbook( " mylogintest.xlsx ") #Read and write by default, if necessary, you can specify write_only and read_only as True 12 b = book.active 13 datadict = {} #Create a dictionary variable 14 maxC = b.max_column #Get the maximum column 15 maxR = b.max_row #Get Maximum line 16 for i in range(1, maxC + 1): # The range starts from 0 by default and ends at -1 of the following parameter, and openpyxl starts from the first line and the first column, so the parameter is 1, maxC +1; It means to traverse the first column to the last column, 17 datadict.setdefault(b.cell(1, i).value) #Set the key value of the dictionary datadict, from the first row and the first column to the end of the first row A column, that is, the title of the first row is set as the key value, i means column 18 # print(datadict) 19 for iin range(2, maxR + 1): #Remove the title of the first row, traverse from the second row to the last row 20 for j in range(1, maxC + 1): # Traverse from the first column to the last column 21 datadict[ b.cell(1, j).value] = b.cell(i, j).value #Set the value corresponding to the key in the dictionary, j represents column 22 # print(datadict) 23 # if datadict["url"] & datadict["check"] != None: 24 self.driver.get(datadict[ " address " ]) #Get the URL value corresponding to the value address from the corresponding key, where the key is the title of the first row of the table, So the title of EXCEL should be written according to the regulations 25 if self.driver.title == datadict[ " check " ]: # If after opening the address , the title value is the same as the value corresponding to the verification Failed ' ) #Write the test failure result to the table 29 book.save( " mylogintest.xlsx " ) # Finally remember to close 30 self.driver.quit() 31 print ( " end " ) 32 33 mylogintest() .openBD()
After the script runs, the table looks like this: