码迷,mamicode.com
首页 > 其他好文 > 详细

设置元素等待.py

时间:2020-01-19 00:27:28      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:sele   method   ted   chrome   字符   情况   驱动程序   time   默认   

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")

element = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"kw")))

element.send_keys("selenium")

#显示等待:
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

driver #浏览器驱动

timeout #最长超时时间,默认以秒为单位

poll_frequency #检测的间隔(步长)时间,默认为0.5S

ignored_exceptions #超时后的异常信息,默认情况下抛NoSuchElementException异常。

WebDriverWait() #一般由until或until_not()方法配合使用

until(method,message=‘‘)
# 调用该方法提供的驱动程序作为一个参数,直到返回值为True。

until_not(method,message = ‘‘)
# 调用该方法提供的驱动程序作为一个参数,直到返回值为False。

#expected_conditions类提供的预期条件判断的方法

title_is #判断当前页面的标题是否等于预期

title_contains #判断当前页面的标题是否包含预期字符串

presence_of_element_located #判断元素是否被加在DOM树里,并不代表该元素一定可见

visibility_of_element_located #判断元素是否可见(可见代表元素非隐藏,并且元素的宽和高都不等于0)

visibility_of # 与上一个方法作用相同,只是上一个方法参数为定位,该方法接收的参数为定位后的元素

presence_of_all_elements_located #判断是否至少有一个元素存在于DOM树中。例如,在个页面中有n个元素的class为"wp",那么只要有一个存在就返回true

text_to_be_present_in_clement #判断某个元素中的text是否包含了预期的字符串

text_to_be_present_in_element_value #判断某个元素的value属性是否包含了预期的字符串

frame_to_be_available_and_switch_to_it #判断该表单是否可以切换进去,如果可以,返回True并且switch进去,否则返回false

invisibility_of_element_located #判断某个元素是否不存在于DOM树或不可见

element_to_be_clickable #判断元素是否可见并且是可以点击的

staleness_of #等到一个元素从DOM树中移除

element_to_be_selecte #判断某个元素是被选中,一般用在下拉列表

element_sclection_state_to_be #判断某个元素的选中状态是否符合预期

element_located_selection_state_to_be #与上一个方法作用相同,只是-上一个方法参数为定位后的元素,该方法要收的参数为定位

alert_is_presen #判断页面上是否存在alert

#隐式等待
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from time import ctime
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
try:
print(ctime())
driver.find_element_by_id("kw22").send_keys("selenium")
except NoSuchElementException as e:
print(e)
finally:
print(ctime())
driver.quit()

设置元素等待.py

标签:sele   method   ted   chrome   字符   情况   驱动程序   time   默认   

原文地址:https://www.cnblogs.com/zhang-da/p/12210688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!