码迷,mamicode.com
首页 > 编程语言 > 详细

python+selenium 发送邮件

时间:2020-02-14 22:26:58      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:web   tar   pytho   edit   elements   enter   遇到   drive   body   

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()

  

python+selenium 发送邮件

标签:web   tar   pytho   edit   elements   enter   遇到   drive   body   

原文地址:https://www.cnblogs.com/a438842265/p/12309701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!