封装定位元素和login函数,在fengzhuang.py中
#coding=utf-8
import time
from selenium import webdriver
#第三版本:封装定位元素
def findID(driver,ID):
return driver.find_element_by_id(ID)
def findLINK(driver,LINK):
return driver.find_element_by_link_text(LINK)
def findCss(driver,css):
return driver.find_element_by_css_selector(css)
def findXpath(driver,xpath):
return driver.find_element_by_xpath(xpath)
def findTagname(driver,tagname):
return driver.find_element_by_tag_name(tagname)
def login(driver,name,pw):
findLINK(driver,u'登录').click()
time.sleep(2)
findID(driver,'TANGRAM__PSP_10__userName').send_keys(name)
findID(driver, 'TANGRAM__PSP_10__password').send_keys(pw)
findID(driver, 'TANGRAM__PSP_10__submit').click()
time.sleep(2)
dirError=findID(driver,'TANGRAM__PSP_10__error').text
driver.close()
assert u'请您输入'in dirError
在baidu.py中调用login函数
#coding=utf-8
import time
from selenium import webdriver
from fengzhuang import login
driver=webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
login(driver,'panda','123456')
time.sleep(3)
driver.quit()