标签:方法 import 正则匹配 rom while 定位器 运行 参数 none
常见问题:
分析原因:
一些同学的解决解决方案:
分析解决方案:
什么是显示等待?
class WebDriverWait(object):
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
"""构造函数,需要一个WebDriver实例并以秒为单位超时
:参数:
- driver - 传入webdriver实例对象
- timeout - 超时时间,单位 秒
- poll_frequency - 程序休眠时间,默认0.5秒
- ignored_exceptions - 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,
则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。
使用例子:
from selenium.webdriver.support.ui import WebDriverWait \n
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
until_not(lambda x: x.find_element_by_id("someId").is_displayed())
"""
def until(self, method, message=''):
"""在规定的超时时间内,调用传入的方法,直到返回的结果为真,否则抛出超时的异常"""
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
def until_not(self, method, message=''):
"""与unitl方法相反,不多解释,项目中基本用不到"""
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if not value:
return value
except self._ignored_exceptions:
return True
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message)
常用
每个类的具体用法查看源码,写的非常清楚
/Python/Python36/Lib/site-packages/selenium/webdriver/support
# 判断页面title等于预期值
title_is
# 判断页面title包含预期字符串
title_contains
# 判断当前url等于预期url
url_to_be
# 判断当前url包含预期字符串
url_matches # 正则匹配 re
url_contains # 包含 in
# 判断当前url不等于预期url
url_changes
# 判断元素显现并定位成功
visibility_of_element_located
# 判断元素显现(不一定能定位,能定位的元素大小需大于0)
visibility_of
# 判断获取文本信息包含预期的文本信息
text_to_be_present_in_element
# 判断获取属性值的信息包含预期的文本信息
text_to_be_present_in_element_value
# 判断iframe可以切换,如果为真则切换,反之返回False
frame_to_be_available_and_switch_to_it
# 判断元素可以点击
element_to_be_clickable
# 判断元素被选中
element_to_be_selected
element_located_to_be_selected
# 判断窗口数量
number_of_windows_to_be
# 判断新窗口打开
number_of_windows_to_be
# 警告框显现
alert_is_present
不常用
presence_of_element_located
presence_of_all_elements_located
visibility_of_any_elements_located
visibility_of_all_elements_located
invisibility_of_element_located
invisibility_of_element(invisibility_of_element_located)
staleness_of
element_selection_state_to_be
element_located_selection_state_to_be
demo.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
# 输入框定位器
locator_of_search = (By.CSS_SELECTOR,'#kw')
# 元素显现并定位成功后,不再等待,反则抛出超时异常
WebDriverWait(driver=driver,timeout=10,poll_frequency=0.5).until(EC.visibility_of_element_located(locator_of_search))
driver.find_element(*locator_of_search).send_keys('测试显示等待')
标签:方法 import 正则匹配 rom while 定位器 运行 参数 none
原文地址:https://www.cnblogs.com/gqv2009/p/12217366.html