python页面加载的等待方式
python页面加载的等待方式
1、显式等待
它指定要查找的节点,然后指定一个最长的等待时间,如果规定时间内加载出来了这个节点,就返回查找的节点;如果规定时间内没有加载出该节点,就抛出超时异常。
fromseleniumimportwebdriver
fromselenium.webdriver.common.byimportBy
fromselenium.webdriver.support.uiimportWebDriverWait
fromselenium.webdriver.supportimportexpected_conditionsasEC
broswer=webdriver.Chrome()
broswer.get('https://www.jd.com/')
wait=WebDriverWait(broswer,20)
input_q=wait.until(EC.presence_of_element_located((By.ID,'key')))
button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.button')))
print(input_q,button)
2、隐式等待
当使用隐式等待执行测试的时候,如果Selenium没有在DOM中找到节点,将继续等待,超出设定时间后,则抛出找不到节点的异常。换句话说,当查找节点而节点并没有出现的时候,隐式等待将等待一段时间再查找DOM,默认时间是0,示例如下:
fromseleniumimportwebdriver
browser=webdriver.Chrome()
browser.implicitly_wait(10)
browser.get('https://www.jd.com/')
input_q=browser.find_element_by_class_name('button')
print(input_q)
以上就是Python页面加载的等待方式,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






