sender.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. from selenium.common import exceptions
  3. from selenium import webdriver
  4. from selenium.webdriver import ActionChains
  5. import time
  6. import sys
  7. import os
  8. from cn.localhost01.util.str_util import print_msg
  9. reload(sys)
  10. sys.setdefaultencoding('utf8') # 对于py2,将ascii改为utf8
  11. is_cmd_run = False
  12. if len(sys.argv) == 2: # cmd启动
  13. is_cmd_run = True
  14. class MailSenderBrowser:
  15. def __init__(self, username, password, password2):
  16. self.__username = username
  17. self.__password = password
  18. self.__password2 = password2
  19. driver = None
  20. action = None
  21. # 是否登录
  22. __is_logined = False
  23. # 淘宝账户
  24. __username = ""
  25. # 登录密码
  26. __password = ""
  27. # 独立密码
  28. __password2 = ""
  29. # 登陆URL
  30. __login_path = "https://mail.qq.com/"
  31. def __login(self):
  32. # 1.进入登陆页面
  33. try:
  34. self.driver.get(self.__login_path)
  35. except exceptions.TimeoutException: # 当页面加载时间超过设定时间,JS来停止加载
  36. self.driver.execute_script('window.stop()')
  37. # 2.切换到登录iframe
  38. login_frame = self.driver.find_element_by_id("login_frame")
  39. self.driver.switch_to_frame(login_frame)
  40. switch_login_a = self.driver.find_element_by_id("switcher_plogin")
  41. switch_login_a.click()
  42. username_input = self.driver.find_element_by_id("u")
  43. username_input.clear()
  44. # 点击,防止腾讯判断机器人操作而网络繁忙
  45. username_input.click()
  46. username_input.send_keys(self.__username)
  47. password_input = self.driver.find_element_by_id("p")
  48. password_input.clear()
  49. # 点击,防止腾讯判断机器人操作而网络繁忙
  50. password_input.click();
  51. password_input.send_keys(self.__password)
  52. self.driver.find_element_by_id("login_button").click()
  53. time.sleep(2)
  54. self.driver.switch_to_default_content()
  55. try:
  56. password2_input = self.driver.find_element_by_id("pp")
  57. if self.__password2 is "":
  58. print_msg("【邮件】邮箱登录失败,请在__init__.py中配置邮箱独立密码!")
  59. return False
  60. password2_input.send_keys(self.__password2)
  61. self.driver.find_element_by_id("btlogin").submit()
  62. except exceptions.NoSuchElementException:
  63. pass
  64. time.sleep(2)
  65. try:
  66. self.driver.find_element_by_id("useralias")
  67. return True
  68. except exceptions.NoSuchElementException:
  69. return False
  70. def send(self, user_to, local_path):
  71. # 切换回窗口
  72. self.driver.switch_to_window(self.driver.window_handles[1])
  73. if self.__is_logined is False:
  74. count = 0
  75. while count < 7:
  76. count += 1
  77. if self.__login() is False:
  78. continue
  79. else:
  80. self.__is_logined = True
  81. break
  82. if count >= 7:
  83. return "邮箱登录失败!"
  84. # 1.写信
  85. write_a = self.driver.find_element_by_id("composebtn")
  86. write_a.click()
  87. time.sleep(1)
  88. # 2.输入邮件信息
  89. form_iframe = self.driver.find_element_by_id("mainFrame")
  90. self.driver.switch_to_frame(form_iframe)
  91. user_to_input = self.driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input")
  92. user_to_input.send_keys(user_to)
  93. subjuct_input = self.driver.find_element_by_id("subject")
  94. subjuct_input.send_keys(u"【尊贵的淘宝买家】")
  95. # 3.超大附件上传
  96. self.driver.find_element_by_class_name("ico_attbig").click()
  97. time.sleep(2)
  98. self.driver.switch_to_default_content()
  99. upload_iframe = self.driver.find_element_by_id("ftnupload_attach_QMDialog__dlgiframe_")
  100. self.driver.switch_to_frame(upload_iframe)
  101. upload_input = self.driver.find_element_by_xpath("//*[@class='upload_btn_center']/a")
  102. upload_input.click()
  103. time.sleep(2)
  104. local_path = local_path.replace("//", "\\").replace("/", "\\").decode('utf-8').encode('gbk')
  105. if is_cmd_run: # 外部cmd批处理文件启动该程序
  106. os.system(os.path.abspath("./cn/localhost01/mail/upload.exe") + r' "firefox" "' + local_path + '"')
  107. else: # 开发软件启动该程序
  108. if os.path.exists(os.path.abspath("./mail/upload.exe")): # main.py位置启动
  109. os.system(os.path.abspath("./mail/upload.exe") + r' "firefox" "' + local_path + '"')
  110. else: # 本文件的main方法位置启动
  111. os.system(os.path.abspath("upload.exe") + r' "firefox" "' + local_path + '"')
  112. # 4.等待上传
  113. while True:
  114. try:
  115. speed = self.driver.find_element_by_xpath("//*[@class='probar_tips']/span[2]/span[1]").text
  116. if speed == "完成":
  117. break
  118. except exceptions.NoSuchElementException:
  119. pass
  120. time.sleep(3)
  121. self.driver.find_element_by_xpath("//*[@id='operate']/a[1]").click()
  122. # 6.发送
  123. self.driver.switch_to_default_content()
  124. form_iframe = self.driver.find_element_by_id("mainFrame")
  125. self.driver.switch_to_frame(form_iframe)
  126. self.driver.find_element_by_xpath("//*[@id='toolbar']/div/a[1]").click()
  127. time.sleep(1)
  128. self.driver.switch_to_default_content()
  129. try:
  130. error = self.driver.find_element_by_class_name("errmsg").text
  131. return error
  132. except exceptions.NoSuchElementException:
  133. return None
  134. if __name__ == "__main__":
  135. # 初始化
  136. MailSenderBrowser.driver = webdriver.Firefox() # 应将浏览器驱动放于python根目录下,且python已配置path环境变量
  137. MailSenderBrowser.action = ActionChains(MailSenderBrowser.driver)
  138. MailSenderBrowser.driver.maximize_window() # 浏览器最大化
  139. MailSenderBrowser.driver.execute_script("window.open('')")
  140. sender_browser = MailSenderBrowser("test@qq.com", "123456", "111111")
  141. sender_browser.send("test02@qq.com", r"C:\Robot_Download\openssh-7.6.rpm.tar")