WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable(ele))

通过追踪代码,可以发现上面的element_to_be_clickable其实也是一个方法,只不过是selenium定义好的方法。

既然可以传方法,那其实也可以传入自定义的方法。

class element_to_be_clickable(object):
""" An Expectation for checking an element is visible and enabled such that
you can click it."""
def __init__(self, locator):
self.locator = locator def __call__(self, driver):
element = visibility_of_element_located(self.locator)(driver)
if element and element.is_enabled():
return element
else:
return False

until方法源码

 def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False."""
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)

举例

from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait class TestHome():
def setup(self):
# 使用Options,需要from selenium.webdriver.chrome.options import Options
chromeOptions = Options()
#9222是端口号,只要是不被占用的端口号都可以
chromeOptions.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
# 使用webdriver,需要from selenium import webdriver
self.driver = webdriver.Chrome(options=chromeOptions)
# 隐式等待,是全局生效的
self.driver.implicitly_wait(3) def teardown(self):
sleep(5) # from time import sleep
self.driver.quit() def test_home(self):
self.driver.find_element(By.ID, 'menu_contacts').click() # 使用By,需要from selenium.webdriver.common.by import By
# self.driver.find_element_by_id('menu_contacts').click()
# sleep(2)
# 显式等待
# WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, '.js_has_member div:nth-child(1) .js_add_member'))) # self.driver.find_element(By.CSS_SELECTOR, '.js_has_member div:nth-child(1) .js_add_member').click() # 循环去点击“添加成员”按钮
# 在until方法种传入自定义方法wait_element
WebDriverWait(self.driver, 10).until(self.wait_element) # 不是WebDriverWait(self.driver,10).until(self.wait_element()) '''
while True:
self.wait_element() '''
self.driver.find_element(By.ID, 'username').send_keys('Tom')
self.driver.find_element(By.ID, 'memberAdd_acctid').send_keys('Tom111')
self.driver.find_element(By.ID, 'memberAdd_phone').send_keys('13838383388')
self.driver.find_element(By.CSS_SELECTOR, '.js_btn_save').click() # 根据页面是否有“姓名”输入框,判断页面是否已经跳转到录入信息页面
def wait_element(self, x): # 复用until方法,所以得加x。
size = len(self.driver.find_elements(By.ID, 'username')) # 调用这个方法时,self.driver.find_elements也会受到隐式等待的影响
if size < 1:
self.driver.find_element(By.CSS_SELECTOR, '.js_has_member div:nth-child(1) .js_add_member').click()
return size >= 1

显式等待until传入自定义方法的更多相关文章

  1. selenium-webdriver中的显式等待与隐式等待

    在selenium-webdriver中等待的方式简单可以概括为三种: 1 导入time包,调用time.sleep()的方法传入时间,这种方式也叫强制等待,固定死等一个时间 2 隐式等待,直接调用i ...

  2. Selenium系列(六) - 强制等待、隐式等待、显式等待

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  3. selenium 隐式等待与显式等待

    1.隐式等待:driver.implicitly_wait() driver = webdriver.Chrome()driver.implicitly_wait(10)     #获取元素时最多会等 ...

  4. 【java+selenium3】隐式等待+显式等待 (七)

    一.隐式等待 -- implicitlyWait 调用方式:driver.manage().timeouts().implicitlyWait(long time, TimeUnit unit); / ...

  5. python+selenium 自动化测试——显式等待详解

    1.前言 之前有提到过等待函数,等待函数分为:强制等待(sleep).隐式等待(implicitly_wait),显示等待(WebDriverWait),这次以显示等待方式专门做一次总结,因为我个人是 ...

  6. Selenium4+Python3系列(六) - Selenium的三种等待,强制等待、隐式等待、显式等待

    为什么要设置元素等待 直白点说,怕报错,哈哈哈! 肯定有人会说,这也有点太直白了吧. 用一句通俗易懂的话就是:等待元素已被加载完全之后,再去定位该元素,就不会出现定位失败的报错了. 如何避免元素未加载 ...

  7. 基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待

    一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...

  8. selenium测试(Java)-- 显式等待(九)

    转自:https://www.cnblogs.com/moonpool/p/5668571.html 显式等待可以使用selenium预置的判断方法,也可以使用自定义的方法. package com. ...

  9. 【亲测显式等待】Selenium:元素等待的4种方法

    Selenium:元素等待的4种方法 1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用.   2.隐式等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉We ...

随机推荐

  1. deepin下启动自己的springcloud服务报错

    java.nio.file.AccessDeniedException: /home/msan/logs/csp/sentinel-record.log.2021-01-04.0.2.lck at s ...

  2. spring boot 整合JPA bean注入失败

    有时候报的错误让你匪夷所思,找错误得学会找根.源头在哪里? 比如:我们刚开始看的错误就是 org.springframework.beans.factory.UnsatisfiedDependency ...

  3. 如何制作图标字体(如何将svg转换为css可用的图标字体)

    转自: 如何制作图标字体(如何将svg转换为css可用的图标字体) 具体描述 在项目开发当中,我们常常遇到需要将获取到的svg转换为,css可用的图标字体,那么具体该如何进行操作呢 具体操作 登录ic ...

  4. SpringBoot跨域

    第一种方法 在Controller类或方法上加上@CrossOrigin元注解 package com.wzq.test.action; import com.wzq.utils.BatchDownF ...

  5. Longhorn,企业级云原生容器分布式存储 - 支持 ReadWriteMany (RWX) 工作负载(实验性功能)

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 企业级云原生容器分布式存储解决方案设计架构和概念 Longhorn 企业级云原生容器分 ...

  6. 怎样在自己的 Web 中加入强大的日志系统系统?slf4j 的日志插件必须要知道!

    对于程序猿来讲,一个应用程序的日志管理是极为重要的.因为,它可以帮助我们随时查看应用程序的运行状态.执行效果等信息,从而监控软件系统.或是根据日志信息解决一些重要的问题. 但是在 Java 应用程序中 ...

  7. 用宏实现HEX到ASCII ,ASCII 到HEX

    #define HEX2ASCII(value, data)  do{  \            value = (value > 0x09)?(value+0x7):value; \     ...

  8. js函数和封装

    $就是jquery对象,$()就是jQuery(),在里面可以传参数,作用就是获取元素 js对象与jQuery对象的区别:jQuery对象是一个数组,jQuery对象转为js对象:[0] 取第一个即可 ...

  9. python 实用技巧:几十行代码将照片转换成素描图、随后打包成可执行文件(源码分享)

    效果展示 原始效果图 素描效果图 相关依赖包 # 超美观的打印库 from pprint import pprint # 图像处理库 from PIL import Image # 科学计算库 imp ...

  10. Mysql索引最佳实践笔记0524

    #mysql5.7 innodb默认存储引擎 一.关于索引二.最佳实践三.避坑实践 一.关于索引 1.索引的作用 -提高查询效率 -数据分组.排序 -避免回表查询 -优化聚集查询 -用于多表join关 ...