小白学python(004)自动登录网易邮箱并写信发送邮件

知识点or坑点:

1、模块化编写测试模块(类似java里的抽象方法,js的函数编写)

from 包名 import 类

调用def定义的方法

2、xpath怎么获取包含部分文本的标签

#获取写信按钮:先获取包含‘写’字的span,再取它的上级li
xx = driver.find_element_by_xpath('//span[contains( text(),"写" )]/parent::li')

1)contains方法为部分包含,模糊查找;(开始找“写信”两个字始终没找到,因为其实文本是“写 信”,坑)
2)python的xpath获取text方法比较特殊,其他属性都是“@属性”获取,文本却是用text()""
3)父节点获取使用parent::

参考原文链接:http://blog.csdn.net/TestLearner/article/details/75569388
3、无法用id、name定位到iframe时,怎么定位frame

	#利用xpath获取frame 再switch_to
    frame = driver.find_element_by_xpath("//iframe[@class='APP-editor-iframe']")
    driver.switch_to.frame(frame)

1) driver.switch_to.frame(‘ID’),driver.switch_to.frame(‘name’)都可以定义子frame,然后就可以操作frame内元素了

2)在无法正常获取frame内元素时,可使用sleep(秒)方法多等待一会,设置1秒一般就够了(坑)

大家都是程序员,以下就贴代码了

首先是模块化编写public包,定义一个class:

from time import sleep

class Login():
    
    #登录
    def user_login(self,driver):
        username = 'testuser'#邮箱用户名
        pwd = 'testpwd123'#密码
        emailInput = driver.find_element_by_name("email")
        emailInput.clear()
        #emailInput.send_keys(username)#火狐执行无效
        email_id = emailInput.get_attribute("id")
        js = 'document.getElementById("'+email_id+'").value="'+username+'"'
        print(js)
        driver.execute_script(js)#执行js
        pwdInput = driver.find_element_by_name("password")
        pwdInput.clear()
        pwdInput.send_keys(pwd)
        dologin = driver.find_element_by_id("dologin")
        dologin.click()

    #退出
    def user_logout(self,driver):
        driver.find_element_by_link_text("退出").click()
        driver.quit()

    #写信
    def write(self,driver):
        #获取写信按钮:先获取包含‘写’字的span,再取它的上级li
        xx = driver.find_element_by_xpath('//span[contains( text(),"写" )]/parent::li')
        sx = driver.find_element_by_xpath('//span[contains( text(),"收" )]/parent::li')
        print('写:',xx.text,'--收:',sx.text)
        print('写:',xx.get_attribute('class'),'--收:',sx.get_attribute('class'))
        xx.click()

        #收件人
        sjr = driver.find_element_by_xpath('//input[@class="nui-editableAddr-ipt"]')
        sjr.clear()
        #sjrId = sjr.get_attribute('id')
        #js = 'document.getElementById('+sjrId+')[email protected]'
        #driver.execute_script(js)
        sjr.send_keys('[email protected]')
        

        #主题
        zt = driver.find_element_by_xpath('//div[@class="bz0"]/div/input[@class="nui-ipt-input"]')
        #print( len(zt) )
        zt.send_keys('我是主题')

        #利用xpath获取frame 再switch_to
        frame = driver.find_element_by_xpath("//iframe[@class='APP-editor-iframe']")
        driver.switch_to.frame(frame)

        #等待1秒,再获取frame内容
        sleep(2)
        
        #内容
        bd = driver.find_element_by_xpath('//body[@class="nui-scroll"]')
        bd.send_keys('我是测试内容002')

        #返回主页面后,需要等待一下,否则可能定位不到元素
        driver.switch_to.default_content()
        sleep(2)

        #随便选择一个发送按钮,点击发送
        fs = driver.find_elements_by_xpath('//span[@class="nui-btn-text"]/parent::div[contains(@class,"nui-btn-hasIcon nui-mainBtn-hasIcon")]')
        print('fs len is ',len(fs))
        fs[0].click()#发送

写完了public包,定义一个Login类,再来看怎么引用

from selenium import webdriver
from public import Login
from time import sleep

#打开谷歌浏览器
dr = webdriver.Chrome()

#隐式等待10秒,等待页面元素加载完毕
dr.implicitly_wait(10)

#打开网页
url = 'http://mail.163.com'
dr.get(url)

#沉睡等待一秒后定位frame
sleep(1)
dr.switch_to.frame('x-URS-iframe')

try:
    #调用登录模块
    Login().user_login(dr)
    print('登录成功')
except BaseException as msg:
    print(msg)
    dr.quit()


#返回主frame
dr.switch_to.default_content()
sleep(1)

try:
    #退出
    Login().write(dr)
    print('写信成功')
except BaseException as msg:
    print(msg)
    sleep(1)
    dr.quit()


sleep(1)
try:
    #退出
    Login().user_logout(dr)
    print('退出成功')
except BaseException as msg:
    print(msg)
    dr.quit()
 

 

 
发布了63 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/yy448671246/article/details/103805640