直接上代码吧!
from selenium import webdriver
from time import ctime
class AutoTest():
def __init__(self):
self.driver=webdriver.Chrome()
self.driver.get("https://email.163.com/")
self.driver.maximize_window() # 窗口最大化
def login(self):
login_frame=self.driver.find_element_by_xpath("//iframe[contains(@id,'x-URS-iframe')]")
#因为webdriver只能在一个页面上对元素进行识别和定位,无法直接定位到iframe嵌套的表单内嵌页面上的元素,所以需要进行switch转换
self.driver.switch_to.frame(login_frame)
#用xpath定位账号输入框
self.driver.find_element_by_xpath("//div[@class='u-input box']/input").send_keys("xiaoxiao")
self.driver.find_element_by_xpath("//*[@name='password']").send_keys("8888")
self.driver.find_element_by_xpath("//a[@id='dologin']").click()
self.driver.switch_to.default_content()
self.driver.implicitly_wait(3) # 隐式等待3秒
def Create_contacts(self):#创建联系人
self.driver.find_element_by_id("_mail_tabitem_1_117text").click()
self.driver.find_element_by_xpath("//div[@class='nui-toolbar-item']/div/span[2]").click()
self.driver.find_element_by_id("input_N").send_keys("筱筱@")
self.driver.find_element_by_xpath("//div[@class='eH0']/dl/dd/div/input").send_keys("123@qq.com")
self.driver.find_element_by_xpath("//dl[@class='ou0']/dd/span/span/b").click()
self.driver.find_element_by_xpath("//div[@id='iaddress_TEL_wrap']/dl/dd/div/input").send_keys("123")
self.driver.find_element_by_id("input_DETAIL").send_keys("加好友")
self.driver.find_element_by_xpath("//div[@class='ph0']/div/dl[4]/dd/div/div/span").click()
self.driver.implicitly_wait(2) # 隐式等待2秒
self.driver.find_element_by_xpath("//div[@id='contact_edit_details']/div/ul/li[3]/span/span/b").click()
self.driver.implicitly_wait(2) # 隐式等待2秒
self.driver.find_element_by_xpath("//div[@class='nui-msgbox-ft']/div[2]/div/span").click()
self.driver.implicitly_wait(2) # 隐式等待2秒
self.driver.find_element_by_xpath("//div[@class='qA0']/span").click()
self.driver.implicitly_wait(2)
self.driver.find_element_by_xpath("//div[@id='iaddress_FAX_wrap']/dl/dd/div[1]/input").send_keys("010")
self.driver.find_element_by_xpath("//div[@id='iaddress_FAX_wrap']/dl/dd/div[2]/input").send_keys("8888")
self.driver.find_element_by_xpath("//div[@id='iaddress_FAX_wrap']/dl/dd/div[3]/input").send_keys("8888")
self.driver.find_element_by_xpath("//div[@id='iaddress_ADR_wrap']/dl/dd/div/input").send_keys("北京市东城区雍和宫")
self.driver.find_element_by_xpath("//div[@id='iaddress_ICQ_wrap']/dl/dt/div").click()
self.driver.find_element_by_xpath("//div[@class='nui-msgbox-bd']/div/div/div/div[2]/a[2]").click()
self.driver.find_element_by_xpath("//div[@id='iaddress_ICQ_wrap']/dl/dd/div/input").send_keys("123")
self.driver.find_element_by_xpath("//div[@id='iaddress_date_wrap']/dl/dd/div[1]/input").send_keys(2020)
self.driver.find_element_by_xpath("//div[@id='iaddress_date_wrap']/dl/dd/div[2]/input").send_keys(11)
self.driver.find_element_by_xpath("//div[@id='iaddress_date_wrap']/dl/dd/div[3]/input").send_keys(27)
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[1]/dd/div/input").send_keys("https://blog.csdn.net/xiaoxiao_chen945")
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[2]/dd/div/input").send_keys("https://blog.csdn.net/xiaoxiao_chen945")
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[3]/dd/div/input").send_keys("技术部")
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[4]/dd/div/input").send_keys("测试架构师")
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[5]/dd/div/input").send_keys("测试专家")
self.driver.find_element_by_xpath("//div[@id='contact_edit_pinfo']/dl[6]/dd/div/input").send_keys("筱筱")
self.driver.find_element_by_xpath("//div[@class='nui-msgbox-ft-btns']/div/span").click()
self.driver.implicitly_wait(3) # 隐式等待3秒
def write_letter_bycontacts(self):#创建完自动发送邮件
self.driver.find_element_by_xpath("//header[@class='frame-head']/nav/div/ul/li/div[3]").click()
self.driver.find_element_by_xpath("//b[@class='nui-ico fn-bg ga0']").click()
self.driver.find_element_by_xpath("//div[@class='kZ0 eB0']/div/div/div[2]/div/input").send_keys("172@qq.com")
self.driver.find_element_by_xpath("//div[@class='kZ0 fu0']/div/div/div/input").send_keys("python自动化学习之旅")
write_frame = self.driver.find_element_by_xpath("//iframe[@class='APP-editor-iframe']")
self.driver.switch_to.frame(write_frame)
self.driver.find_element_by_xpath("//body[@class='nui-scroll']/p").send_keys("人生苦短,我用python")
self.driver.switch_to.default_content()
self.driver.find_element_by_xpath("//footer[@class='jp0']/div/span[2]").click()
sendemail=AutoTest()
sendemail.login()#登录
sendemail.Create_contacts()#创建联系人
sendemail.write_letter_bycontacts()#发送邮件