做网站需要什么权限,网站开发前端和后端怎么连接,山西省的网站,.net做网站安全吗引言#xff1a;在做UI自动化的过程中#xff0c;我们有时候为了等待元素的出现#xff0c;需要加一些等待时间来帮助#xff0c;但是有时候时间加的过多或者过少#xff0c;这个没有办法判断#xff0c;今天就介绍几种等待时间#xff0c;我们看看那种适合我们 一、强制…引言在做UI自动化的过程中我们有时候为了等待元素的出现需要加一些等待时间来帮助但是有时候时间加的过多或者过少这个没有办法判断今天就介绍几种等待时间我们看看那种适合我们 一、强制等待看到名称就应该知道强制等待就是设置多少秒就必须等待多少秒才能继续往下面操作time.sleep()def sleep(seconds): # real signature unknown; restored from __doc__ sleep(seconds) 延迟指定的秒数 pass使用方法# 直接在需要等待的地方添加 time.sleep(10)二、隐式等待隐式等待 implicitly_wait?() 默认参数的单位为妙设置一个等待时间它并不影响脚本的执行速度。当脚本执行到某个元素定位是如果元素可以定位则继续执行如果元素定位不到则它将以轮询的方式不断地判断元素是否被定位到。假设在第六秒定位到了元素则继续执行若直到超出设置的时长10秒还没有定位到元素则抛出异常。def implicitly_wait(self, time_to_wait): Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout. :Args: - time_to_wait: Amount of time to wait (in seconds) :Usage: driver.implicitly_wait(30) if self.w3c: self.execute(Command.SET_TIMEOUTS, { implicit: int(float(time_to_wait) * 1000)}) else: self.execute(Command.IMPLICIT_WAIT, { ms: float(time_to_wait) * 1000})使用方法# 在需要等待的地方直接添加 driver.implicitly_wait(10)三、Activity等待Activity等待 app特有一种等待通过Activity的出现来帮助我们进行判断是否到达这个页面然后进行一系列的操作 通过wait_activity 进行判断def wait_activity(self, activity, timeout, interval1): 等待一个activity直到在规定时间内activity出现 This is an Android-only method. :Args: - activity - target activity - timeout - max wait time, in seconds - interval - sleep interval between retries, in seconds try: WebDriverWait(self, timeout, interval).until( lambda d: d.current_activity activity) return True except TimeoutException: return False使用方法:直接在需要等待元素出现的地方添加# 添加activity后面加上等待的时间超过时间就报错 driver.wait_activity(com.ali.user.mobile.login.ui.UserLoginActivity,30)四、显示等待显示等待本来准备等到写selenium教程的时候在介绍感觉后面会用这里就直接给大家进行介绍了。 如果对软件测试、接口测试、自动化测试、面试经验交流。感兴趣可以加测试交流群还会有同行一起技术交流。先看源码def __init__(self, driver, timeout, poll_frequencyPOLL_FREQUENCY, ignored_exceptionsNone): driver: 返回一个webdriver实例化 timeout设置一个超时时长S poll_frequency循环读取元素的时间默认是0.5s 使用方法 : from selenium.webdriver.support.ui import WebDriverWait n element WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id(someId)) n is_disappeared WebDriverWait(driver, 30, 1, (ElementNotVisibleException)). n until_not(lambda x: x.find_element_by_id(someId).is_displayed()) self._driver driver self._timeout timeout self._poll poll_frequency # avoid the divide by zero if self._poll 0: self._poll POLL_FREQUENCY exceptions list(IGNORED_EXCEPTIONS) if ignored_exceptions is not None: try: exceptions.extend(iter(ignored_exceptions)) except TypeError: # ignored_exceptions is not iterable exceptions.append(ignored_exceptions) self._ignored_exceptions tuple(exceptions)从源码中分许出来三个参数的作用driver返回一个webdriver实例化timeout设置一个超时时长poll_frequentcy循环读取元素时间使用方法# 导入方法 from selenium.webdriver.support.ui import WebDriverWait element WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id(someId))