定位元素的封装

Posted panda901

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定位元素的封装相关的知识,希望对你有一定的参考价值。

封装定位元素和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()
 

以上是关于定位元素的封装的主要内容,如果未能解决你的问题,请参考以下文章

Selenium Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element(代码片段

appium---封装定位

定位元素的封装

如何使绝对定位的元素响应?

selenium对元素定位和操作方法的封装

Selenium2+python自动化63-二次封装(click/send_kesy)