• 使用Selenium发邮件附件


    发邮件可以使用SMTP协议实现程序去发送,但附件的不能太大,一般不超过20M。
    以下使用Selenium模拟发送邮件,跳过这个限制,网上找了很多资料,都没有完整实现的,那么自己实现一个,以下代码用Python完成:

    import sys
    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    import win32gui
    import win32con
    
    # 在这里导入浏览器设置相关的类
    from selenium.webdriver.edge.options import Options
     
    # 无可视化界面设置 #
     
    edge_options = Options()
    # 使用无头模式
    edge_options.add_argument('--headless')
    # 禁用GPU,防止无头模式出现莫名的BUG
    
    
    driver = webdriver.Edge()
    url = 'http://mail.163.com/'
    driver.get(url)
    driver.maximize_window()
    acount_num="请用自已的账号,不要抄"
    passwd_str="请用自已的密码,不要抄"
    # 163登陆框是使用iframe进行嵌套的,所以需要先切换到该iframe
    driver.switch_to.frame(0)
    acount = driver.find_element(By.NAME,'email')
    acount.clear()
    acount.send_keys(acount_num)
    
    passwd = driver.find_element(By.NAME,'password')
    passwd.clear()
    passwd.send_keys(passwd_str)
    
    # 30天内免登陆
    toEdit=driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[2]/form/div/div[9]/div")
    ActionChains(driver).move_to_element(toEdit).click().pause(2).perform()
    try:
        time.sleep(3)
        click_button = driver.find_element(By.ID,'dologin')
        click_button.click()
        time.sleep(60)
    except:
        print("except");
    
    # 点写邮件
    #driver.execute_script("$(\"#_mail_component_76_76\").click()")
    toEdit=driver.find_element(By.XPATH,"/html/body/div[1]/nav/div[1]/ul/li[2]")
    ActionChains(driver).move_to_element(toEdit).click().pause(2).perform()
    time.sleep(3)
    # 收件人
    toEdit=driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[1]/div[1]/div/div[2]")
    # 收件人的地址,要改哦
    ActionChains(driver).move_to_element(toEdit).click().pause(2).send_keys("tomail@qq.com").perform()
    
    #主题
    toEdit=driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[2]/div[1]/div/div/input")
    ActionChains(driver).move_to_element(toEdit).click().pause(2).send_keys("Erp-Data").perform()
    
    # 附件
    toEdit=driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[3]/div[1]/div[2]")
    ActionChains(driver).move_to_element(toEdit).click().pause(2).perform()
    
    dialog = win32gui.FindWindow('#32770', u'打开')  # 对话框
    ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None) 
    ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
    Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)  # 上面三句依次寻找对象,直到找到输入框Edit对象的句柄
    button = win32gui.FindWindowEx(dialog, 0, 'Button', None)  # 确定按钮Button
    
    win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, 'D:\Code\GoPathNew\src\sendmail\main.go')  # 往输入框输入绝对地址,这里是附件,要改哦
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)  # 按打开按钮
    
    # 内容
    driver.switch_to.frame(driver.find_element(By.XPATH,"//iframe[contains(@class,'APP-editor-iframe')]"))
    driver.find_element(By.XPATH,"/html/body").send_keys("xxx");
    
    driver.switch_to.default_content()
    
    toEdit=driver.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[2]/header/div/div[1]/div/span[2]")
    ActionChains(driver).move_to_element(toEdit).click().pause(2).perform()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    另外还有C# 版本的:

    https://download.csdn.net/download/aa_qq110/88508640
    
    • 1
  • 相关阅读:
    js 基础 (ES 模块)
    ChatGPT第八讲
    机器人控制算法—如何使用C++读取pgm格式的栅格地图并转化为ROS地图格式的data?
    C#的LINQ select查询、where过滤、group分组、join关联
    黑马redis学习记录Ⅲ SpringDataRedis客户端
    Unity vscode 官方debug
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.12 Object类详解
    Flutter组件--OverflowBox、SizedOverflowBox(子组件超出父组件裁剪)
    [CISCN2019 华北赛区 Day1 Web2]ikun
    漏刻有时百度地图API实战开发(2)文本标签显示和隐藏的切换开关
  • 原文地址:https://blog.csdn.net/aa_qq110/article/details/134252011