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. IP笔记

    自动专用IP地址APIPA的范围是B类地址块169.254.0.0--169.254.255.255

  2. L298N的接线和详细使用方法

    文章说明: 名词概念(为了方便易懂,我就通俗的表达): 逻辑电压:控制板子执行程序的电压. 驱动电压:输出口AB的电压. 逻辑电流:驱动板执行程序的电流. 驱动电流:输出口AB的电流. 本人调试此款L ...

  3. noip36

    开场先看一遍题面,凭着错误的感觉t3叫naive是一个原因,312开局.然后就死的很惨. T1 朴素暴力40pts,细想就有80pts,然而我只写了十分钟左右就爬回T3了,所以... 其实都是借口 正 ...

  4. HTTP协议之:HTTP/1.1和HTTP/2

    目录 简介 HTTP/1.1 HTTP/2 传输模式对比 流优先级 缓冲区溢出处理 预测资源请求 压缩 总结 简介 HTTP的全称是Hypertext Transfer Protocol,是在1989 ...

  5. FTP 大文件传输问题 解决

    问题描述 将附件上传至FTP服务器,但是当附件过大,其大小为几百兆或1-2G时,会出现上传失败的现象 FTP上传原理解析 这里我们使用的是.Net的FtpWebRequest 创建FtpClient ...

  6. qt 定义插件

    定义的接口----------------------------------------------#ifndef REGEXPINTERFACE_H #define REGEXPINTERFACE ...

  7. 天翼云安装jdk(注意有坑)

    1.下载jdk8 查看Linux位数,到oracle官网下载对应的jdk ① sudo uname --m  确认32位还是64位 ② https://www.oracle.com/technetwo ...

  8. Linux命令:ps -ef |grep java

    一.ps -ef |grep java 查看包含"java"的所有进程 二.涉及命令详解 ps命令将某个进程显示出来(是LINUX下最常用的也是非常强大的进程查看命令) grep命 ...

  9. Javascript - Vue - vuex

    vuex 这是一个与vue配套的公共数据管理工具,可以将一些需要共享的数据保存到vuex中,以此方便项目中的任何组件都可以从vuex中得到共享数据.cnpm i vuex -S 装包 读取数据 //在 ...

  10. 关于Junit中Assert已经过时

    在junit4.12之后,Assert就过时了,提供了TestCase来取代: 同样在TestCase中原本比较常见的一些方法也已经取消了,例如:assertNotEquals.assertThat. ...