python3 selenium 自动化 登录搜狐邮箱之 函数的二次封装和模块的调用及函数的调用

先建一个 sohuEmailPublic.py 文件,对函数进行封装,让它作为公共的模块开使用
代码如下:

from selenium import webdriver
from time import sleep

'''定义一个叫SohuEmail的类
操作步骤如下:
1.打开网页
2.输入账号、输入密码 并点击登录
3.退出邮箱账号
4.关闭浏览器'''

class SohuEmail():
    def __init__(self,dr):
        self.dr = dr

#1.打开网页
    def openUrl(self):
        self.dr.get('https://mail.sohu.com/fe/#/login')
        sleep(3)
        self.dr.maximize_window()
        sleep(2)

#2.输入账号和密码并登录,传入account,password两个变量参数
    def login(self,account,password):
        self.dr.find_element_by_xpath('//*[@id="theme"]/form/div[1]/div[1]/input').send_keys(account)
        self.dr.find_element_by_xpath('//*[@id="theme"]/form/div[2]/div[1]/input').send_keys(password)
        self.dr.find_element_by_xpath('//*[@id="theme"]/form/div[5]/input').click()

# 3.退出邮箱账号
    def logout(self):
        sleep(3)
        self.dr.find_element_by_xpath('//*[@id="addSkinClass"]/div[1]/div[3]/ul/li[5]').click()

#4.关闭浏览器
    def close_brower(self):
        self.dr.quit()

# 定义一个函数,把前面的所有的函数再次封装
    def all_actions(self,account,password):
        self.openUrl()
        self.login(account,password)
        self.logout()
        self.close_brower()


再建一个 sohuEmaillogin.py文件,对函数进行调用,代码如下:

from selenium import webdriver
from time import sleep
from sohuEmailPublic import SohuEmail

dr =webdriver.Chrome()

'''按照步骤一个个调用,这样做一步步的调用,很麻烦,有没有简单快捷的办法呢?'''
# L=SohuEmail(dr)                             #实例化类
# L.openUrl()                                 #打开网页
# L.login('[email protected]','xxxxxxxxx')       #输入账号和密码并登录
# L.logout()                                  #退出邮箱账号
# L.close_brower()                            #关闭浏览器


'''简单快捷的办法是有的,以下我只需一步就调用上面的动作'''

L=SohuEmail(dr)
L.all_actions('[email protected]','xxxxxxxxx')

猜你喜欢

转载自blog.csdn.net/xiezhiming1234/article/details/82222346
今日推荐