0


selenium等待机制

selenium等待机制

影响元素加载的外部因素

1.计算机的性能

不同的计算机配置不同,这是我们经常忽略的一个问题。如果你在一台高配置的计算机上编写调试脚本,却将脚本发送到一台低配置的计算机去执行,此时计算机可能会花很长的时间来渲染被测页面。这就会发生一种奇怪的现象:本地调试都正常,但分布式执行整个测试集合的时候,总有那么几条用例会大概率报错。

2.服务器的性能

首先解释一下,这里的服务器是指运行被测操作系统的应用服务器或数据库服务器。一般来说,服务器的性能不会太差,但是如果该测试服务器并非Web
UI自动化测试的专属测试环境,例如这套测试环境上同时部署了缺陷管理工具、代码管理工具、接口管理系统、局域网邮件系统等,并且某个时刻存在大量并发用户请求,服务器就可能在处理Web
UI自动化测试发起的请求时花费更多的时间,错误由此产生。总之,你需要注意该问题。

3.浏览器的性能

如果被测操作系统中大量使用了JavaScript,那么不同的浏览器执行脚本所需要的渲染时间可能差异很大。在学习过程中,你可以分别尝试在Chrome、Firefox、IE等不同的浏览器上执行同样的脚本,其执行效率可能差异很大。所以并不建议使用Selenium做浏览器的兼容性测试,这一点将在第15章中讲解。

4.网络因素

如果被测操作系统中大量使用了Ajax,那么网络是否稳定将非常重要。因为解析Ajax请求需要过多的时间的话,那么测试时浏览器将花更长的时间来重新渲染前端。在实际项目中,如果你在测试环境中编写的脚本执行良好,但是换到准生产或者生产环境执行却时常出现元素定位失败的情况,这时候你可能需要考虑一下网络的问题了。

总之,在UI自动化测试过程中,导致元素加载失败或加载过缓的原因有多种。如果不能正确处理这些问题,测试脚本的稳定性将大打折扣。

强制等待

1.强制等待

强制等待就是不管怎样都要等待固定的时间。很多文章或图书喜欢将Python的time模块提供的sleep方法称为“强制等待”,但我首先要说明的是,sleep是Python提供的功能,并非Selenium WebDriver提供的方法。

很多因素,这些因素可能会导致需要等待的时间不一样。例如某个页面元素,一般情况下你只需要等待3秒(sleep(3)),被操作的元素就可以出现,可一旦出现意外,当前页面元素在3秒内没有加载出来,脚本就会发生错误。部分测试人员在面对这种问题时,第一反应是延长等待时间,可是到底要等待多少秒呢?在单条执行测试用例的时候多等待5秒、10秒甚至20秒可能无所谓,可是随着测试脚本的持续集成,用例数量越来越多,执行一轮自动化测试脚本的时间可能会多到项目组不堪重负。“这脚本执行的速度还不如人工来得快!费大力气开发测试脚本的意义是什么?我觉得自动化测试的意义不大了。”当出现这种声音的时候,自动化测试人员总是尴尬的。说这么多的目的是让你记住,sleep方法可以帮我们调试代码和观察脚本执行情况。除此之外,应该尽量少用或不用它。

2.页面加载超时机制

另外,还需要思考一个问题:如果页面发生了错误,我们不能一直等待下去吧,是不是需要给页面设置一个最多等待多少秒的限制?Selenium提供了set_page_load_timeout方法,用于设置等待页面加载的时间,如果超时页面未加载完毕则报错。

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_page_load_timeout(2)
driver.get('https://www.ptpress.com.cn/')
driver.quit()

隐性等待

隐性等待就是WebDriver会在约定好的时间内持续地检测元素是否找到,一旦找到目标元素就执行后续的动作,如果超过了约定时间还未找到元素则报错。Selenium提供了implicitly_wait方法用来设置隐性等待。在讲解其用法之前,我们先来讲段历史。隐性等待最初并不包含在WebDriverAPI中,它其实是旧版Selenium 1 API中遗留下来的。后来隐性等待被放到了WebDriverAPI中,原因是来自社区的强烈抗议(社区成员习惯了该API)。这并非WebDriver开发者的本意。不管如何,让我们先来看看隐性等待的用法吧。

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id('kw1').send_keys('Storm')
driver.quit()

当百度首页被打开后,就立即报错了,原因是id='kw1’的元素没有找到(故意写错,模拟元素找不到的效果)。

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.baidu.com/')
driver.find_element_by_id('kw1').send_keys('Storm')
driver.quit()

执行代码,当打开百度首页后,因为找不到id='kw1’的元素,等待10秒后才会报错。但是在第5秒或第7秒的时候能找到元素的话,它就会继续执行。这个功能太好用了,你只需要在浏览器初始化的时候加上这么一句短短的代码“driver.implicitly_wait(10)”,WebDriver就会在指定的时间内持续检测和搜寻DOM,以便查找那些不是立即加载成功的元素。这对解决由于网络延迟或利用Ajax动态加载元素所导致的元素偶尔找不到的问题非常有效。注意,它和sleep是有本质区别的,sleep是固定等待多长时间,不管元素有没有提前找到,都必须等待时间消耗完才会执行下面的动作;而“driver.implicitly_wait(10)”要智能得多,它会持续地检测元素是否找到,一旦找到了就执行后续的动作,节省了很多时间。

注意▶ “driver.implicitly_wait(10)”和“sleep(10)”还有个不同点,前者一旦设置了隐性等待,它就会作用于实例化WebDriver的整个生命周期,而“sleep(10)”只会对当前行有效。

隐性等待的缺点:
(1)隐性等待会减缓测试速度
(2)隐性等待需要等待整个页面加载完成
(3)隐性等待会干扰显性等待

显性等待

WebDriver提供了WebDriverWait类和expected_conditions类来实现显性等待。WebDriverWait类用来定义超时时间、轮询频率等;expected_conditions类提供了一些预制条件,作为测试脚本进行后续操作的判断依据。

1.WebDriverWait类

先来通过PyCharm看一下WebDriverWait类

# Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreements.  See the NOTICE file# distributed with this work for additional information# regarding copyright ownership.  The SFC licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License.  You may obtain a copy of the License at##   http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied.  See the License for the# specific language governing permissions and limitations# under the License.import time
import typing
from typing import Callable
from typing import Generic
from typing import Literal
from typing import TypeVar
from typing import Union

from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.types import WaitExcTypes
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement

POLL_FREQUENCY:float=0.5# How long to sleep in between calls to the method
IGNORED_EXCEPTIONS: typing.Tuple[typing.Type[Exception]]=(NoSuchElementException,)# default to be ignored.

D = TypeVar("D", bound=Union[WebDriver, WebElement])
T = TypeVar("T")classWebDriverWait(Generic[D]):def__init__(
        self,
        driver: D,
        timeout:float,
        poll_frequency:float= POLL_FREQUENCY,
        ignored_exceptions: typing.Optional[WaitExcTypes]=None,):"""Constructor, takes a WebDriver instance and timeout in seconds.

        :Args:
         - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote) or a WebElement
         - timeout - Number of seconds before timing out
         - poll_frequency - sleep interval between calls
           By default, it is 0.5 second.
         - ignored_exceptions - iterable structure of exception classes ignored during calls.
           By default, it contains NoSuchElementException only.

        Example::

         from selenium.webdriver.support.wait 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 =float(timeout)
        self._poll = poll_frequency
        # avoid the divide by zeroif self._poll ==0:
            self._poll = POLL_FREQUENCY
        exceptions =list(IGNORED_EXCEPTIONS)if ignored_exceptions:try:
                exceptions.extend(iter(ignored_exceptions))except TypeError:# ignored_exceptions is not iterable
                exceptions.append(ignored_exceptions)
        self._ignored_exceptions =tuple(exceptions)def__repr__(self):returnf'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'defuntil(self, method: Callable[[D], Union[Literal[False], T]], message:str="")-> T:"""Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        screen =None
        stacktrace =None

        end_time = time.monotonic()+ self._timeout
        whileTrue: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.monotonic()> end_time:breakraise TimeoutException(message, screen, stacktrace)defuntil_not(self, method: Callable[[D], T], message:str="")-> Union[T, Literal[True]]:"""Calls the method provided with the driver as an argument until the \
        return value evaluates to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`, or
                  ``True`` if `method` has raised one of the ignored exceptions
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        end_time = time.monotonic()+ self._timeout
        whileTrue:try:
                value = method(self._driver)ifnot value:return value
            except self._ignored_exceptions:returnTrue
            time.sleep(self._poll)if time.monotonic()> end_time:breakraise TimeoutException(message)
  • driver,必选参数,WebDriverWait中必须传入一个driver。
  • timeout,必选参数,WebDriverWait中必须传入一个timeout,决定最多轮询多少秒。
  • poll_frequency,可选参数,轮询频率,即每隔多长时间去查一下后续的条件是否满足,默认间隔为0.5秒。
  • ignored_exceptions,可选参数,决定忽略的异常。如果在调用until或until_not的过程中抛出这个元组中的异常,则不中断代码,而是继续等待;如果抛出的是这个元组外的异常,则中断代码并抛出异常。默认只有NoSuchElementException异常。

2.WebDriverWait类提供的方法

在这里插入图片描述
WebDriveWait提供了一种until方法和一种until_not方法

untile

defuntil(self, method: Callable[[D], Union[Literal[False], T]], message:str="")-> T:"""Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        screen =None
        stacktrace =None

        end_time = time.monotonic()+ self._timeout
        whileTrue: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.monotonic()> end_time:breakraise TimeoutException(message, screen, stacktrace)

until方法,和字面意思一样,在WebDriverWait类规定的时间(第二个参数)内,每隔一定的时间(第三个参数)调用一下method方法,直到其返回值不为False。如果超时就抛出TimeoutException的异常,异常信息为message。

untile_not

defuntil_not(self, method: Callable[[D], T], message:str="")-> Union[T, Literal[True]]:"""Calls the method provided with the driver as an argument until the \
        return value evaluates to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`, or
                  ``True`` if `method` has raised one of the ignored exceptions
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        end_time = time.monotonic()+ self._timeout
        whileTrue:try:
                value = method(self._driver)ifnot value:return value
            except self._ignored_exceptions:returnTrue
            time.sleep(self._poll)if time.monotonic()> end_time:breakraise TimeoutException(message)

until_not方法,表示在规定的时间内,每隔一段时间调用一下method方法,直到其返回False。如果超时则抛出TimeoutException的异常,异常信息为message。

显性等待的语法格式

WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时后返回的信息)

这里需要特别注意的是until或until_not中的可执行方法method参数,很多人传入了WebElement对象,错误示范如下:

WebDriverWait(driver,10).until(driver.find_element_by_id('kw'), message)# 错误

这是错误的用法,这里的参数一定要是可以调用的,即这个对象一定有__call__方法,否则会抛出异常。

正确示例:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
ele=WebDriverWait(self.__driver,iwait).until(EC.element_to_be_clickable((locators[0],locators[1])))
ele.click()

3.expected_conditions模块方法

在这里,你可以用Selenium提供的 expected_conditions 模块中的各种条件,
也可以用 WebElement 的is_displayed、is_enabled、is_selected方法,或者也可以用自己封装的方法。接下来我们看一下Selenium提供的预期条件有哪些。expected_conditions是Selenium的一个模块,你可以使用下面的语句导入该模块(as
EC的意思是为该模块取个别名,方便后续引用)。

from selenium.webdriver.support import expected_conditions as EC

expected_conditions预置的判断条件

判断条件描述title_is(title)判断页面的title是否与预期的一致,全部匹配返回True 否则返回Falsetitle_contains(title)判断页面的title是否包含预期的值,包含返回True 否则返回Falsepresent_of_element_located(locator)用于检查某个元素是否存在于页面 DOM 中,注意元素并不一定可见。locator 用来查找元素,找到后返回该元素,返回对象是 WebElementurl_contains(ur)检查当前url是否包含期望的子字符串。url_matches(ur)检查当前url是否包含期望的子字符串。从头匹配url_to_be(ur)检查当前url是否匹配url_changes(ur)检查当前url不匹配返回Truevisibility_of_element_located(locator)检查元素是否存在于的DOM上的期望页面并且可见。可见性意味着不仅显示元素而且还具有大于0的高度和宽度。visibility_of(WebElement)参数是元素,判断元素是否在页面 DOM 中,如果在并且可见返回 True,否则返回 Falsepresence_of_any_elements_located参数是 locator,判断根据定位器找到的所有元素,如果都是的话返回值是列表,定位不到或者不全是的话则报错(找到即可不必全可见)visibility_of_all_elements_located参数是 locator,判断根据定位器找到的所有符合条件的元素是否都是可见元素,如果都是的话返回值是列表,定位不到或者不全是的话则报错visibility_of_any_elements_located参数是 locator,根据定位器至少应该能定位到一个可见元素,返回值是列表,如果定位不到则报错text_to_be_present_in_element(locator, text)判断 text 是否出现在元素中,两个参数,返回一个布尔值element_to_be_clickable(locator)判断这个元素是否可见并且可单击,满足条件返回 True,否则返回 Falsestaleness_of(element)判断这个 element是否仍然在 DOM 中,如果在返回 False,否则返回 True。也就是说页面刷新后元素不存在了,就返回 Trueelement_to_be_selected(element)判断元素是否被选中,传入的参数是 element,如果被选中,那么返回值是这个元素element_located_ to_be_selected(locator)判断元素是否被选中,传入的参数是 locator,如果被选中,那么返回值是这个元素element_selection_state_to_be/element, is_selected)传入两个参数,第一个是元素,第二个是状态element_located_selection_state_to_be(locator, is_selected)传入两个参数,第一个是定位器,第二个是状态number_of__windows_to _be(num_windows)判断窗口的数量是否是预期的值,返回值是布尔值new_window_is_opened(current_handles)传入当前窗口的句柄,判断是否有新窗口打开,返回布尔值alert_is_present(driver)判断是否有 alert,如果有则切换到该 alert,否则返回 False

示例

示例一:判断页面title
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")try:
     ele = WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))# 判断titleprint(type(ele))# 输出返回值类型print(ele)# 输出返回值
     driver.find_element_by_link_text('地图').click()# 如果匹配,就执行该语句except Exception as e:raise e #有异常抛出finally:
     driver.quit()# 最后退出driver
示例二:判断页面元素能否定位presence_of_all_elements_located(locator)
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")try:
     eles = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located ((By.TAG_ NAME,'input')))#检查元素是否找到print(len(eles))# 返回列表中包含的元素数量print(type(eles))# 返回值类型print(eles)# 返回值except Exception as e:raise e
finally:
     driver.quit()
示例三:判断url
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")try:
     ele = WebDriverWait(driver,10).until(EC.url_contains('www.baidu.com/'))#检查url是否包含# ele = WebDriverWait(driver,10).until((EC.url_matches('www.baidu.com/'))) # 检查包含# ele = WebDriverWait(driver, 5).until(EC.url_to_be("https://www.baidu.com/")) # 检查完全匹配# ele = WebDriverWait(driver, 5).until(EC.url_changes("https://www.baidu.com/a")) # 检查不完全匹配print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例四:判断元素是否可见
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")try:
     ele = WebDriverWait(driver,5).until(EC.visibility_of_all_elements_located((By.ID,'kw')))# ele = WebDriverWait(driver, 5).until(EC.visibility_of_any_elements_located((By.TAG_NAME, 'input')))print(ele)print(len(ele))print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例五:判断并切换iframe
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>本地iframe</title></head><body><iframeid="iframe1"src="https://www.baidu.com/"></iframe></body></html>
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
html_file ='File:///'+ os.getcwd()+ os.sep +'myhtml8_1.html'
driver.get(html_file)try:
     ele = WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe1")))# ele = WebDriverWait(driver, 5).until(EC.visibility_of_any_elements_located((By.TAG_NAME, 'input')))
     driver.find_element_by_id('kw').send_keys('Storm')
     sleep(2)print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例六:判断元素是否包含text文本
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')try:
     ele = WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.LINK_TEXT,'地图'),'图'))
     sleep(2)print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例七:判断元素属性value是否包含text
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.find_element_by_id('kw').send_keys('Storm')try:
     ele = WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.ID,'kw'),'Storm'))
     sleep(2)print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例八:判断页面元素是否可被单击
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')try:
     ele = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT,'地图')))
     sleep(2)print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例九:判断页面是否刷新
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
ele1 = driver.find_element_by_id('kw')
driver.refresh()# 这里刷新页面try:
     ele = WebDriverWait(driver,10).until(EC.staleness_of(ele1))
     sleep(2)print(ele)print(type(ele))except Exception as e:raise e
finally:
     driver.quit()
示例十:判断页面元素是否被选中
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>本地复选框</title></head><body>
    复选框:<inputtype="checkbox"id="id1"></body></html>
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.select import Select
import os

driver = webdriver.Chrome()
html_file ='File:///'+ os.getcwd()+ os.sep +'myhtml8_2.html'
driver.get(html_file)
ele = driver.find_element_by_id('id1')
ele.click()
     ele1 = WebDriverWait(driver,10).until(EC.element_to_be_sel
     sleep(2)print(ele1)print(type(ele1))except Exception as e:raise e
finally:
     driver.quit()
示例十一:类似示例十,通过定位器判断元素是否被选中
rom selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.select import Select
import os

driver = webdriver.Chrome()
html_file ='File:///'+ os.getcwd()+ os.sep +'myhtml8_2.html'
driver.get(html_file)
ele = driver.find_element_by_id('id1')
ele.click()try:
    ele1 = WebDriverWait(driver,10).until(EC.element_located_to_be_selected((By.ID,'id1')))
     sleep(2)print(ele1)print(type(ele1))except Exception as e:raise e
finally:
     driver.quit()
示例十二:同样是判断元素是否被选中,传递两个参数
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
html_file ='File:///'+ os.getcwd()+ os.sep +'myhtml8_2.html'
driver.get(html_file)
ele = driver.find_element_by_id('id1')
ele.click()try:# ele1 = WebDriverWait(driver, 10).until(EC.element_located_selection_state_to_be((By.ID, 'id1'), is_selected=True))
     ele1 = WebDriverWait(driver,10).until(EC.element_selection_state_to_be(ele, is_ selected=True))
     sleep(2)print(ele1)print(type(ele1))except Exception as e:raise e
finally:
     driver.quit()
示例十三:判断当前窗口的数量
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')try:
     ele1 = WebDriverWait(driver,10).until(EC.number_of_windows_to_be(1))
     sleep(2)print(ele1)print(type(ele1))except Exception as e:raise e
finally:
     driver.quit()
示例十四:通过判断窗口句柄的数量,判断是否有新窗口打开
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/')
cur_window_handles = driver.window_handles  # 获取当前窗口句柄print("当前窗口句柄:{}".format(cur_window_handles))
driver.find_element_by_link_text('Window Open Test').click()try:
     ele1 = WebDriverWait(driver,10).until(EC.new_window_is_opened(cur_window_handles))
     sleep(2)print(ele1)print(type(ele1))except Exception as e:raise e
finally:
     driver.quit()
示例十五:判断是否有Alert窗口
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Alert学习</title></head><body><h2>Alert Test</h2><scripttype="text/javascript">functionshowAlert(){alert(document.f1.t1.value);}</script><formname="f1"><inputtype="text"name="t1"value="Alert Message"><br><br><inputtype="button"name="b1"value="Click For Alert"onclick="showAlert()"><br></form></body></html>
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
import os

driver = webdriver.Chrome()
html_file ='File:///'+ os.getcwd()+ os.sep +'myhtml8_3.html'
driver.get(html_file)
driver.find_element_by_name('b1').click()try:
     ele1 = WebDriverWait(driver,10).until(EC.alert_is_present())
     sleep(2)print(ele1)print(type(ele1))
     driver.switch_to.alert.accept()
     sleep(2)except Exception as e:raise e
finally:
     driver.quit()

自定义等待条件

虽然expected_conditions模块提供了丰富的预定义的等待条件,但如果还是不能满足需求的话,你还可以借助lambda表达式来自定义预期等待条件。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')try:
     ele1 = WebDriverWait(driver,10).until(lambda x:x.find_element_by_id('kw'))
     ele1.send_keys('Storm')except Exception as e:raise e
finally:
     driver.quit()

本文转载自: https://blog.csdn.net/weixin_42439274/article/details/136531285
版权归原作者 一个双鱼座的测开 所有, 如有侵权,请联系我们删除。

“selenium等待机制”的评论:

还没有评论