coding=utf-8
from selenium import webdriver
import time
path = 'D:/Language/Python/Python38/chromedriver.exe'
driver = webdriver.Chrome(path)
driver.implicitly_wait(30)#所有元素最长等待60s
try:
#最大化窗口
driver.maximize_window()
driver.get('https://mail.qq.com/cgi-bin/loginpage')
#定位框架
driver.switch_to.frame("login_frame")
driver.find_element_by_id('switcher_plogin').click()
#输入邮箱登录账号、密码
driver.find_element_by_id('u').send_keys("账号")
driver.find_element_by_id('p').send_keys("密码")
#点击登录
driver.find_element_by_id('login_button').click()
driver.switch_to.default_content()
#点击写信
driver.find_element_by_id('composebtn').click()
driver.switch_to.frame('mainFrame')
time.sleep(2)
#写入收件人邮箱、主题、上传附件
driver.find_element_by_xpath("//[@id='toAreaCtrl']/div[2]/input").send_keys('收件人邮箱')
driver.find_element_by_xpath("//[@id='subject']").send_keys('主题')
driver.find_element_by_name('UploadFile').send_keys("附件地址")
time.sleep(2)
#写信内容是个大坑,一定要注意这里还有一个动态框架,可以通过定位class_name来获取这个框架,进入框架后才可以找到内容的元素
driver.switch_to.frame(driver.find_element_by_class_name("qmEditorIfrmEditArea"))
a = driver.find_element_by_tag_name("body")
a.click()
a.send_keys('test')
driver.switch_to.default_content()
driver.switch_to.frame("mainFrame")
#点击发送
driver.find_element_by_name("sendbtn").click()
finally:
a = "n"
while a == "n":
a = input("是否关闭浏览器驱动?y/n
")
if a == "n":
pass
else:
time.sleep(3)
driver.quit()