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. 【网络编程】TCPIP-8-套接字的多种选项

    目录 前言 8. 套接字的多种选项 8.1 API getsockopt(); & setsockopt(); 8.2 套接字选项 8.3 缓冲区相关可选项 8.4 端口复用 8.4.1 ti ...

  2. 【mysql】单表使用索引常见的索引失效

    1. 全值匹配我最爱 全值匹配我最爱指的是,查询的字段按照顺序在索引中都可以匹配到! SQL 中查询字段的顺序,跟使用索引中字段的顺序,没有关系.优化器会在不影响SQL 执行结果的前提下,给 你自动地 ...

  3. UWP - 介绍App Service 与新功能

    App Service 是一种背景工作运行的服务,提供给其他Apps 使用就像Web Service.它本身无使用介面(UI-less),允许Apps 在同一个设备被引用,甚至Windows 10 1 ...

  4. Web安全-信息收集

    信息收集 前言:在渗透测试过程中,信息收集是非常重要的一个环节,此环节的信息将影响到后续成功几率,掌握信息的多少将决定发现漏洞的机会的大小,换言之决定着是否能完成目标的测试任务.也就是说:渗透测试的思 ...

  5. MAC下Jetbrains编译器无法打开问题解决

    这段时间不知道怎么回事,每次打开Rider必定闪退,毫无头绪,只好暂时放弃使用Rider,试用了一段时间Visual Studio. 可惜...虽然大学时候觉得VS天下第一,但是用惯了JB的编译器,再 ...

  6. 【C语言】第1章 程序设计与C语言

    第1章 程序设计与C语言 程序:一组计算机能识别和执行的 指令. 计算机语言:人和计算机交流信息的.计算机和人都能识别的语言 C语言允许用两种注释方式: //:单行注释 可单独占一行 可出现在一行中其 ...

  7. 优先队列PriorityQueue&Lambda&Comparator

    今天翻阅<Labuladuo的算法小抄>时发现在使用优先队列的PriorityQueue解决一道hard题时(leetCode 23),出现了如下代码: ListNode mergeKLi ...

  8. shell 字符串判空

    2021-09-01 1. 字符串判空主要用到两个参数 -z 判断字符串为空否 -n 判断字符串不为空 2. 实例 #!/bin/bash PID=`date` if [ -z "$PID& ...

  9. 存储系统管理(一)——Linux系统的设备和分区管理

    1.设备名称的理解 /dev/sda1? sata硬盘,a1表示第一块硬盘中的第一个分区 /dev/cdrom 光驱 /dev/mapper/*? 系统中的虚拟设备 2.发现系统中的设备 ? fdis ...

  10. vue element-ui el-date-picker 数据可以更改,但是前端不显示的更改后的数据问题

    template: <el-form-item label="有效时间:" prop="validTime">                    ...