zoukankan      html  css  js  c++  java
  • selenium-判断元素是否可见(五)

    很多 case 在运行时都会出现页面还没加载完成,但是脚本已经跑完,并且报未找到元素

    这是就需要增加判断,在预定的时间内如果页面显示了某元素后再让脚本继续执行,则为判断元素是否可见或者说页面是否显示了某元素

    以百度首页,搜素框为例:

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com/')
    baidu_input = driver.find_element_by_id('kw')
    EC.visibility_of_element_located(baidu_input)
    
    driver.close()
    EC.visibility_of_element_located(baidu_input) 只是判断元素是否可见,若果这样写明显存在不合理的地方。如果代码运行很快,页面还未加载完就会出现该元素可见找不到。
    所以通常需要结合
    WebDriverWait 一起使用
    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com/')
    baidu_input = (By.ID, 'kw')
    WebDriverWait(driver,10).until(EC.visibility_of_element_located(baidu_input))
    
    driver.close()

    查看 WebDriverWait 类,他需要传入driver,超时时间timeout,而 unit 只需要传入定位元素,如下代码 WebDriverWait 类所示

    所以在使用 WebDriverWait 时需要对元素定位使用 By 定位,剔除通过 driver 再定位的方法,如上代码所示

    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
    from selenium.common.exceptions import NoSuchElementException
    from selenium.common.exceptions import TimeoutException
    
    POLL_FREQUENCY = 0.5  # How long to sleep inbetween calls to the method
    IGNORED_EXCEPTIONS = (NoSuchElementException,)  # exceptions ignored during calls to the method
    
    
    class WebDriverWait(object):
        def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
            """Constructor, takes a WebDriver instance and timeout in seconds.
    
               :Args:
                - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
                - 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.ui import WebDriverWait 
    
                element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) 
    
                is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)). 
    
                            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)
    
        def __repr__(self):
            return '<{0.__module__}.{0.__name__} (session="{1}")>'.format(
                type(self), self._driver.session_id)
    
        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)
    
        def until_not(self, method, message=''):
            """Calls the method provided with the driver as an argument until the 
            return value is False."""
            end_time = time.time() + self._timeout
            while True:
                try:
                    value = method(self._driver)
                    if not value:
                        return value
                except self._ignored_exceptions:
                    return True
                time.sleep(self._poll)
                if time.time() > end_time:
                    break
            raise TimeoutException(message)
    View Code
    元素是否可见的方法

      visibility_of_element_located : 判断某个元素是否可见
      invisibility_of_element_located : 判断某个元素是否不存在或不可见
      visibility_of : 判断元素是否可见,通过 driver 查找元素,如:EC.visibility_of(driver.find_element_by_id('kw'))
      visibility_of_all_elements_located() :判断定位的所有元素都存在于DOM树中并且可见,可存在则以list形式返回
      visibility_of_any_elements_located() : 判断定位的所有元素中,至少有一个存在于DOM树中并且可见,list形式返回存在的元素

  • 相关阅读:
    一步步用新浪SAE免费教大家搭建个人博客(wordpress 3.0.4 for SAE )
    欢迎大家来访西北狼网络乌托邦
    教大家如何让新浪SAE上安装wordpress实现伪静态
    CSDN 600万用户数据信息泄露并道歉
    推荐5款好用的屏幕录像软件
    IPv6无法解决全部安全问题
    详解STP以及工作过程
    如何在WordPress中实现彩色标签云
    EIGRP和RIP的一个综合性很强的实验(变态实验之一)
    查看系统等待的sql
  • 原文地址:https://www.cnblogs.com/tynam/p/10343145.html
Copyright © 2011-2022 走看看