python+selenium 发送邮件
import time from selenium import webdriver from selenium.webdriver import ChromeOptions from selenium.webdriver.common.keys import Keys from getpass import getpass def run(): driver.get(url='https://mail.qq.com/') # 遇到iframe,需要切换 iframe_list = driver.find_elements_by_tag_name('iframe') # print(iframe_list) driver.switch_to.frame(iframe_list[1]) # 输入用户名、密码并登录 driver.find_element_by_class_name('inputstyle').send_keys(user) driver.find_element_by_class_name('password').send_keys(pwd) driver.find_element_by_class_name('password').send_keys(Keys.ENTER) # 点击写信 driver.find_element_by_id('composebtn').click() iframe = driver.find_element_by_id('mainFrame') driver.switch_to.frame(iframe) # 输入收件人、主题信息 driver.find_element_by_xpath('//*[@id="toAreaCtrl"]/div[2]/input').send_keys(to) driver.find_element_by_xpath('//*[@id="subject"]').send_keys(theme) # 遇到body iframe iframe_body = driver.find_element_by_class_name('qmEditorIfrmEditArea') driver.switch_to.frame(iframe_body) # 输入文本 driver.find_element_by_tag_name('body').send_keys(content) # 切出body iframe driver.switch_to.default_content() driver.switch_to.frame(iframe) # 点击发送按钮 driver.find_element_by_xpath('//*[@id="toolbar"]/div/a[1]').click() # time.sleep(3) # driver.quit() if __name__ == '__main__': to = '' # 你想发的邮箱 theme = 'python自动发送邮件' content = '不用回,我就试试,哈哈哈。' user = '' # QQ账号 pwd = getpass('密码:') # QQ密码 # 创建 option 对象 option = ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) # 创建浏览器对象 driver = webdriver.Chrome(options=option) ''' driver = webdriver.Chrome(executable_path=r'E:\chromedriver.exe') ''' driver.implicitly_wait(10) run()