Login_info.txt:[email protected]163. Com,xxxx[email protected]163. com,xxxxmain.py:#Note: The encoding format#algorithm:(1) to define multiple methods to perform different actions separately (2) Stitching a long string, methods and parameters separated by commas (,), separated by the return "\ n" between each method (3) cut the long string into such a format as "method name, Parameter", strip (). Split ("\ n") (4) Use Split (",") Determine the length of the parameter, for example "sleep,5" parameter length len (step) ==2, stitching into command, with a similar "sleep (5)", it is best to use command="%s (%s)"% (step[0],step[1]) in such a way that it is not easy to splice errors (5execute the command with the Eval function fromSeleniumImportWebdriverImport TimeImportChardetdriver=""defOpen_browser ():GlobalDriver Driver=webdriver. Ie (executable_path="C:\\iedriverserver")defvisit_url (URL):Globaldriver Driver.get (URL)defSwitch_to_frame (frame_xpath_expression):GlobalDriver Time.sleep (3) Frame=Driver.find_element_by_xpath (frame_xpath_expression) Driver.switch_to.frame (frame)definput (element_xpath_expression,content):Globaldriver Element=Driver.find_element_by_xpath (element_xpath_expression) element.clear () Element.send_keys (content)defClick (element_xpath_expression):Globaldriver Element=Driver.find_element_by_xpath (element_xpath_expression) Element.click ()defAssert_str_in_page_source (s):GlobalDriverassertSinchDriver.page_sourcedefget_out_of_frame (): Driver.switch_to_default_content ()defsleep_time (seconds): Time.sleep (float (seconds) )defClose ():Globaldriver Driver.quit () with open ("C:\\login_info.txt") as F: forDatainchF:username_data,pwd_data=data.strip (). Split (",") Steps="""open_browservisit_url,http://mail.163.comswitch_to_frame,//iframe[@id = ' X-urs-iframe ']input,//input[@ Placeholder= ' email account or phone number ' and @name = ' email '],%sinput,//input[@placeholder = ' password '],%sclick,//a[@id = ' Dologin ']sleep_ Time,5get_out_of_frameassert_str_in_page_source, exit Close"""%(username_data,pwd_data) Steps=steps.split ("\ n") PrintSteps Command="" forStepinchSteps:step=step.split (",") ifLen (step) ==1: Command="%s ()"%(step[0])elifLen (step) ==2: Command="%s (U '%s ')"% (Step[0],step[1].decode ("Utf-8")) elifLen (step) ==3: Command="%s ('%s ', U '%s ')"% (step[0],step[1],step[2]) Print "command:", Command eval (command)
"Webdriver Automation" encapsulates the operation of 163 login mailboxes into multiple methods to execute