使用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("[email protected]").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()

另外还有C# 版本的:

https://download.csdn.net/download/aa_qq110/88508640

你可能感兴趣的:(Selenium,selenium,测试工具)