世事洞明皆学问,人情练达即文章。这篇文章主要讲述seleniumselenium find_element()详解相关的知识,希望能为你提供帮助。
selenium定位方式
# 元素属性定位(3种)
driver.find_element_by_id(‘id’)
driver.find_element_by_name(name)
driver.find_element_by_class_name(class_name)
# 超链接定位(2种)
driver.find_element_by_link_text(link_text)
driver.find_element_by_partial_link_text(partial_link_text)
# 元素标签,元素路径,CSS选择器
driver.find_element_by_tag_name(tag_name)
driver.find_element_by_xpath(xpath)
driver.find_element_by_css_selector(css_selector)
但我们看下源码:
D:\\Program Files\\python27\\Lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py
可以看到,上面那些定位方式,实际上都是调用的driver.find_element(by, value)
我们继续看下find_element()的源码,大部分方法最终全是通过By.CSS_SELECTOR来实现的查找
那By又都有哪些条件呢?
D:\\Program Files\\Python27\\Lib\\site-packages\\selenium\\webdriver\\common\\by.py
继续看源码,可以看到下面8种
ID = “id”
XPATH = “xpath”
LINK_TEXT = “link text”
PARTIAL_LINK_TEXT = “partial link text”
NAME = “name”
TAG_NAME = “tag name”
CLASS_NAME = “class name”
CSS_SELECTOR = “css selector”
所以我们以后就都只用find_element()就好了,因为最终实际上也都是调用的这个方法。
简单封装一下,以后就记住find_element()一个方法即可。
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from appium import webdriver
class BasePage(object):
def split_locator(self, locator):
"""
分解定位表达式,如css,.username,拆分后返回css selector和定位表达式.username(class为username的元素)
:param locator: 定位方法+定位表达式组合字符串,如css,.username
:return: locator_dict[by], value:返回定位方式和定位表达式
"""
by = locator.split(,)[0]
value = https://www.songbingjia.com/android/locator.split(,)[1]
locator_dict =
id: id,
name: name,
class: class name,
tag: tag name,
link: link text,
plink: partial link text,
xpath: xpath,
css: css selector,
if by not in locator_dict.keys():
raise NameError("wrong locator!id,name,class,tag,link,plink,xpath,css,exp:id,username")
return locator_dict[by], value
def wait_element(self, locator, sec=30):
"""
等待元素出现
:param locator: 定位方法+定位表达式组合字符串,用逗号分隔,如css,.username
:param sec:等待秒数
"""
by, value = https://www.songbingjia.com/android/self.split_locator(locator)
try:
WebDriverWait(self.driver, sec, 1).until(lambda x: x.find_element(by=by, value=https://www.songbingjia.com/android/value),message=element not found!!!)
log.info(u等待元素:%s % locator)
return True
except TimeoutException:
return False
except Exception, e:
raise e
def get_element(self, locator, sec=60):
"""
获取一个元素
:param locator: 定位方法+定位表达式组合字符串,用逗号分隔,如css,.username
:param sec:等待秒数
:return: 元素可找到返回element对象,否则返回False
"""
if self.wait_element(locator, sec):
by, value = https://www.songbingjia.com/android/self.split_locator(locator)
try:
element = self.driver.find_element(by=by, value=https://www.songbingjia.com/android/value)
log.info(u获取元素:%s % locator)
return element
except Exception, e:
raise e
else:
return False
def get_elements(self, locator):
"""
获取一组元素
:param locator: 定位方法+定位表达式组合字符串,用逗号分隔,如css,.username
:return: elements
"""
by, value = https://www.songbingjia.com/android/self.split_locator(locator)
try:
elements = WebDriverWait(self.driver, 60, 1).until(lambda x: x.find_elements(by=by, value=https://www.songbingjia.com/android/value))
log.info(u获取元素列表:%s % locator)
return elements
except Exception, e:
raise e
以后调用,先定义元素botton,在用例中查找时,调用方法即可
botton="id,su"
get_element(botton)# 页面上id为su的元素
【seleniumselenium find_element()详解】整体框架源码:
??? https://github.com/songzhenhua/selenium_ui_auto/blob/master/page_object/base_page.py???
推荐阅读
- JAVASession
- axios|vue项目 使用axios封装request请求(一)
- Git入门第二讲(创建版本库)
- --PostgreSQL怎么正确的开始POSTGRESQL 调优的活动1
- PostgreSQL读书会一期系统目录表1
- Redis哪些地方需要调整 杂谈
- PostgreSQL“千万” 不能做五大 foolish 操作
- 防火墙基础之安全策略和GRE隧道
- PostgreSQLpg_qualstats 解决索引缺失,添加索引