一、函数的定义,并引用
public_model.py
#登陆
def login(driver):
driver.find_element_by_id("idInput").clear()
driver.find_element_by_id("idInput").send_keys('username')
driver.find_element_by_id("pwdInput").clear()
driver.find_element_by_id("pwdInput").send_keys('password')
driver.find_element_by_id("loginBtn").click()
#退出
def logout(driver):
driver.find_element_by_link_text("退出").click()
driver.quit()
diaoyong_publicmodel.py 调用模块化的函数文件
#coding=utf-8
#调用public文件的登录与退出函数
from selenium import webdriver
from public_model import login,logout
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("http://www.126.com")
#登陆
login(driver)
#退出
logout(driver)
二、使用类进行模块化及引用
public_class.py
#encoding=utf-8
class Login():
def __init__(self):
self.driver=driver
def login(self):
self.driver.find_element_by_id("idInput").clear()
self.driver.find_element_by_id("idInput").send_keys('username')
self.driver.find_element_by_id("pwdInput").clear()
self.driver.find_element_by_id("pwdInput").send_keys('password')
self.driver.find_element_by_id("loginBtn").click()
#退出
def logout(self):
self.driver.find_element_by_link_text("退出").click()
self.driver.quit()
diaoyong_publicclass.py
#coding=utf-8
from selenium import webdriver
#调用public文件的Login类里的函数
from public_class import Login
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("http://www.126.com")
#登陆
Login(driver).login()
#退出
Login(driver).logout()