Python+selenium自动循环发邮件

Python源代码如下:

# coding=utf-8
from selenium import webdriver
from time import sleep
from random import randint


def auto_throw(target_mail, username, password):
    """自动循环发邮件"""
    driver = webdriver.Chrome()
    driver.get("https://mail.qq.com")
    driver.set_window_size(1100, 580)         # 设置窗口大小
    sleep(1)
    # 切换iframe表单
    driver.switch_to.frame("login_frame")
    driver.find_element_by_xpath('//*[@id="u"]').send_keys(username)       # 用户名
    driver.find_element_by_xpath('//*[@id="p"]').send_keys(password)       # 密码
    driver.find_element_by_xpath('//*[@id="login_button"]').click()        # 登录
    sleep(1)
    # 将表单切回到最外层
    driver.switch_to.default_content()
    sleep(2)
    m = 0
    while True:
        try:
            m += 1
            driver.switch_to.default_content()
            driver.find_element_by_xpath('//*[@id="composebtn"]').click()      # 写信
            sleep(1)
            driver.switch_to.frame("mainFrame")
            # driver.find_element_by_xpath('//div[@title="陌生人"]/a').click()      # 好友分组
            driver.find_element_by_xpath('//a[@title="%s"]'%target_mail).click()    # 选择好友
            sleep(1)
            driver.find_element_by_xpath('//input[@title="主题是一封邮件的标题,可不填。"]').send_keys(
                "第%d封邮件发送成功! 随机代号为%d!"%(m, randint(100000,1000000)))      # 主题
            driver.find_element_by_xpath('//a[@name="sendbtn"]').click()        # 发送
            print("第%d封邮件发送成功!"%m)
            sleep(1)
        except:
            print("发送失败!")
            sleep(1)


if __name__ == '__main__':
    print("--------自动循环发邮件脚本--------")
    target_mail = input("请输入对方邮箱:")
    username = input("输入你的QQ邮箱用户名:")
    password = input("输入你的QQ邮箱登录密码:")
    auto_throw(target_mail, username, password)

 

转载于:https://www.cnblogs.com/chenbin93/p/9108528.html

你可能感兴趣的:(Python+selenium自动循环发邮件)