Selenium之发送163邮件
目录
直接上代码:
import time
import datetime
from selenium import webdriver
from selenium.webdriver.support.wait import webdriverwait # 等待页面加载某些元素
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
def login(user, pwd):
""" 登录163邮箱 """
# 由于可以扫码登录,而我们选择用户名和密码登录,所以,要点击 密码登录
time.sleep(1)
wait.until(EC.presence_of_element_located((By.ID, 'switchAccountLogin'))).click()
# 进入iframe,因为有多个iframe,所以获取的是数组,在分析页面后,数组0索引的iframe是登陆的iframe
time.sleep(3)
iframe = driver.find_elements_by_tag_name('iframe')
# print(iframe)
'''
[
<selenium.webdriver.remote.webelement.WebElement (session="3f92dbd96e72746e7d27d64e6b412318", element="0.855888743369456-2")>,
<selenium.webdriver.remote.webelement.WebElement (session="3f92dbd96e72746e7d27d64e6b412318", element="0.855888743369456-3")>,
<selenium.webdriver.remote.webelement.WebElement (session="3f92dbd96e72746e7d27d64e6b412318", element="0.855888743369456-4")>,
<selenium.webdriver.remote.webelement.WebElement (session="3f92dbd96e72746e7d27d64e6b412318", element="0.855888743369456-5")>
]
'''
driver.switch_to.frame(iframe[0])
<span class="hljs-comment"># 获取用户名和密码标签,并输入对应的值</span>
time.sleep(<span class="hljs-number">1</span>)
driver.find_element_by_class_name(<span class="hljs-string">'dlemail'</span>).send_keys(user)
time.sleep(<span class="hljs-number">2</span>)
driver.find_element_by_class_name(<span class="hljs-string">'dlpwd'</span>).send_keys(pwd)
time.sleep(<span class="hljs-number">2</span>)
driver.find_element_by_id(<span class="hljs-string">'dologin'</span>).click()
def send_mail():
""" 发送163邮件,需要传递163的用户名和密码,收件人和内容 """
<span class="hljs-keyword">try</span>:
<span class="hljs-comment"># 第1步,执行登陆</span>
login(user, pwd)
<span class="hljs-comment"># 第2步,点击写信按钮</span>
wait.until(EC.presence_of_element_located((By.ID, <span class="hljs-string">'_mail_component_24_24'</span>))).click()
<span class="hljs-comment"># driver.find_element_by_id('_mail_component_24_24').click()</span>
<span class="hljs-comment"># 第3步,获取收件人,主题,内容框标签,写入内容</span>
time.sleep(<span class="hljs-number">1</span>)
<span class="hljs-comment"># 3.1 填写收件人</span>
wait.until(EC.presence_of_element_located((By.CLASS_NAME, <span class="hljs-string">'nui-editableAddr-ipt'</span>))).send_keys(addr) <span class="hljs-comment"># 收件人</span>
time.sleep(<span class="hljs-number">2</span>)
<span class="hljs-comment"># 3.2 填写主题</span>
title = driver.find_elements_by_class_name(<span class="hljs-string">'nui-ipt-input'</span>)
<span class="hljs-comment"># print(11111, title)</span>
title[<span class="hljs-number">2</span>].send_keys(theme) <span class="hljs-comment"># 主题</span>
<span class="hljs-comment"># title.send_keys(theme) # 主题</span>
<span class="hljs-comment"># 3.3 进入content所在iframe,填写内容</span>
time.sleep(<span class="hljs-number">1</span>)
content_iframe = driver.find_element_by_class_name(<span class="hljs-string">'APP-editor-iframe'</span>)
driver.switch_to.frame(content_iframe)
<span class="hljs-comment"># 虽然nui-scroll这个类名在整个网页中有多个,但是这个iframe中只有一个,所以我们直接send_keys就行</span>
nui_scroll = wait.until(EC.presence_of_element_located((By.CLASS_NAME, <span class="hljs-string">'nui-scroll'</span>)))
<span class="hljs-comment"># print(22222222, nui_scroll) # <selenium.webdriver.remote.webelement.WebElement (session="106a6f5778c14568827014435ddcfcd9", element="0.07847410617283446-1")></span>
nui_scroll.send_keys(content)
<span class="hljs-comment"># 第4步,因为发送按钮不在此时的iframe中,所以要先退出iframe,才能点击发送按钮</span>
<span class="hljs-comment"># 4.1 退出iframe</span>
time.sleep(<span class="hljs-number">1</span>)
driver.switch_to.default_content()
<span class="hljs-comment"># 4.2 点击发送按钮</span>
time.sleep(<span class="hljs-number">1</span>)
<span class="hljs-comment"># 这个发送按钮的类名有多个,最好for循环一下,因为有坑,发送按钮是第3个,前面还有两个空标签,但是前端检查中看不到</span>
driver.find_elements_by_class_name(<span class="hljs-string">'nui-btn-text'</span>)[<span class="hljs-number">2</span>].click()
<span class="hljs-keyword">finally</span>:
<span class="hljs-comment"># 关闭浏览器</span>
time.sleep(<span class="hljs-number">3</span>)
driver.quit()
<span class="hljs-comment"># 截止2019-6-11,代码无误</span>
if name == 'main':
<span class="hljs-keyword">from</span> getpass <span class="hljs-keyword">import</span> getpass
user = input(<span class="hljs-string">"邮箱: "</span>).strip() <span class="hljs-comment"># 填写你的163账号</span>
pwd = getpass(<span class="hljs-string">'密码: '</span>) <span class="hljs-comment"># 填写你的163密码</span>
<span class="hljs-comment"># 获取driver</span>
driver = webdriver.Chrome()
wait = webdriverwait(driver, <span class="hljs-number">10</span>)
<span class="hljs-comment"># driver.maximize_window()</span>
<span class="hljs-comment"># 发请求</span>
driver.get(<span class="hljs-string">'https://mail.163.com/'</span>)
addr = <span class="hljs-string">"1206180814@qq.com"</span> <span class="hljs-comment"># 收件人</span>
theme = <span class="hljs-string">'我是你爸爸'</span> <span class="hljs-comment"># 主题</span>
content = <span class="hljs-string">'天不生我李淳罡,剑道万古如长夜 ————\n{}'</span>.format(datetime.datetime.Now()) <span class="hljs-comment"># 发送内容</span>
send_mail()