Today in the study of selenium encountered two questions, here to record a bit;
After using the UnitTest framework to organize the test cases, expanding the functionality is what I want to do most, so I decided to add the email feature.
Using Python's smtplib, you can easily complete the email;
Details of how to use:
Beginner's Tutorial--Email tutorial
The problems identified during the coding process are as follows:
One, the issue of sending mail and text at the same time
The tutorial does not send the body and the message at the same time, later by looking at the source found Mimemultipart () class can be loaded by the attach method of the parameters you constructed into a list
Here to send their own code for your reference, tap
ImportSmtplib fromEmail.mime.textImportMimetext#message body fromEmail.headerImportHeader#the title of the message fromEmail.mime.multipartImportMimemultipart#Mail Attachments#Create a Send message classclasssend_email ():def __init__(Self,sender ="[email protected]", receiver ="[email protected]", Subject='Python Email Test', SmtpServer ='smtp.qiye.163.com', username ='[email protected]', Password='xxxxxxxxxxx'): Self.sender=Sender Self.receiver=receiver Self.subject=subject Self.smtpserver=SmtpServer Self.username=username Self.password=PassworddefSend (Self,html_zhengwen_url ="", Html_fujian_url =""): " "Html_fujian_url: Is the local address of the message body" " " "Html_fujian_url: Is the local address of the message attachment" " """The following is the creation of the HTML body, tested by"""with open (Html_zhengwen_url,'R', encoding='Utf-8') as E:#HTML message bodyHtml_msg ="'. Join (E.readlines ()) Att2= Mimetext (Html_msg,'HTML','Utf-8')#writing an HTML-type message bodyatt2['Subject'] = Header (Self.subject,'Utf-8') ##创建一个smtp实例SMTP =Smtplib. SMTP ()"""The following is the creation of an attachment, tested by""" #Create an attachment instanceMSG2 =Mimemultipart () msg2["Subject"] = Header (Self.subject,'Utf-8') #Construction Annex 1ATT1 = Mimetext (open (Html_fujian_url,'RB'). Read (),'Base64','gb2312') att1["Content-type"] ='Application/octet-stream'att1["content-disposition"] ='attachment; filename= "text.html"' #filename here can be arbitrarily written, what name to write, what name is displayed in the message " "at the same time send the message and the body, only need to assign a value to the Attach method" "Msg2.attach (ATT1) Msg2.attach (ATT2)Try: Smtp.connect (self.smtpserver) smtp.login (Self.username, Self.password) Smtp.sendmai L (Self.sender, Self.receiver, msg2.as_string ())#the msg.as_string here can be changed to MSG2 exceptException as E:Print(e)finally: Smtp.quit ()
Second, address format conversion problem
Because the foundation is not strong, cause in the simplest string concatenation is the noisy joke.
Problem and address related
For example, assign a value to variable a to an address
line = "11.html"
A = "C:\Program Files (x86) \python36-32\scripts\" +line
Because \ is an escape character, so this variable is not directly recognized by the program, so I want to directly in front of the string to add R, the results found that this method does not work, will be error.
A = (r"C:\Program Files (x86) \python36-32\scripts\") +line
Later thought to change into a double backslash, so that you can call the normal.
A= "C:\ \Program Files (x86) \python36-32\Scripts\ \"+line
In fact, it can be so,
A = (r"C:\Program Files (x86) \python36-32\scripts\") +line
Today's record is here.
Remember: Keep the lessons in mind and lay a solid foundation.
Selenium-python Issue Diary