我们可以使用 pyautoit 模块将文件附加到 gmail 吗?
Can we attach a file to gmail using pyautoit module?
import os
import sys
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
type(browser)
Get_webpage=browser.get('https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail#identifier')
user_name = browser.find_element_by_id('Email')
user_name.send_keys("username")#Enter your username
time.sleep(2)
next = browser.find_element_by_id('next')
next.submit()
time.sleep(5)
password = browser.find_element_by_id('Passwd')
password.send_keys("password")#enter your password
password.submit()
time.sleep(5)
compose = browser.find_element_by_xpath("//div[@role='button']")
compose.click()
time.sleep(5)
Attach_file = browser.find_element_by_xpath("//div[@role='button']")
我能够登录 gmail.I 能够撰写邮件但我无法附加任何内容 file.Can 有人建议我附加文件的方法吗?可以使用 selenium 或我必须使用 pyautoit 模块吗?
如果您试图避免 "right" 做事方式,您将走上一条非常艰难的道路。放弃目前使用硒的方法,潜入冷水中,这并不难。
这是一个发送带有附件的电子邮件的工作示例,一旦您理解了MIME
您就可以对邮件做任何您想做的事情。
# -*- coding: iso-8859-1 -*-
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
msg = MIMEMultipart()
msg['Subject'] = 'Email From Python'
msg['From'] = 'sai@gmail.com'
msg['To'] = 'whatever@whatever.com'
# That is what u see if dont have an email reader:
msg.preamble = 'Multipart massage.\n'
# This is the textual part:
part = MIMEText("Hello im sending an email with a PDF from a python program")
msg.attach(part)
# This is the binary part(The Attachment):
part = MIMEApplication(open("networkanalyze.pdf","rb").read())
part.add_header('Content-Disposition', 'attachment', filename="file.pdf")
msg.attach(part)
# Create an instance in SMTP server
smtp = SMTP("smtp.gmail.com:587")
smtp.ehlo()
smtp.starttls()
smtp.login("sai@gmail.com", "mySuperSecretPassword")
smtp.close()
# Send the email
smtp.sendmail(msg['From'], msg['To'], msg.as_string())
import os
import sys
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
type(browser)
Get_webpage=browser.get('https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en&service=mail#identifier')
user_name = browser.find_element_by_id('Email')
user_name.send_keys("username")#Enter your username
time.sleep(2)
next = browser.find_element_by_id('next')
next.submit()
time.sleep(5)
password = browser.find_element_by_id('Passwd')
password.send_keys("password")#enter your password
password.submit()
time.sleep(5)
compose = browser.find_element_by_xpath("//div[@role='button']")
compose.click()
time.sleep(5)
Attach_file = browser.find_element_by_xpath("//div[@role='button']")
我能够登录 gmail.I 能够撰写邮件但我无法附加任何内容 file.Can 有人建议我附加文件的方法吗?可以使用 selenium 或我必须使用 pyautoit 模块吗?
如果您试图避免 "right" 做事方式,您将走上一条非常艰难的道路。放弃目前使用硒的方法,潜入冷水中,这并不难。
这是一个发送带有附件的电子邮件的工作示例,一旦您理解了MIME
您就可以对邮件做任何您想做的事情。
# -*- coding: iso-8859-1 -*-
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
msg = MIMEMultipart()
msg['Subject'] = 'Email From Python'
msg['From'] = 'sai@gmail.com'
msg['To'] = 'whatever@whatever.com'
# That is what u see if dont have an email reader:
msg.preamble = 'Multipart massage.\n'
# This is the textual part:
part = MIMEText("Hello im sending an email with a PDF from a python program")
msg.attach(part)
# This is the binary part(The Attachment):
part = MIMEApplication(open("networkanalyze.pdf","rb").read())
part.add_header('Content-Disposition', 'attachment', filename="file.pdf")
msg.attach(part)
# Create an instance in SMTP server
smtp = SMTP("smtp.gmail.com:587")
smtp.ehlo()
smtp.starttls()
smtp.login("sai@gmail.com", "mySuperSecretPassword")
smtp.close()
# Send the email
smtp.sendmail(msg['From'], msg['To'], msg.as_string())