zoukankan      html  css  js  c++  java
  • selenium学习笔记——driver.get(url) 页面加载时间太长

    # 两个同时设置才行
    # 实现效果:加载状态停止,进行代码下一步操作
    driver.set_page_load_timeout(10)
    driver.set_script_timeout(10)  # 这两种设置都进行才有效
    try:
        driver.get("https://shopee.co.id/search?keyword=jam%20tangan&page=" + str(page) + "&sortBy=" + rankBY)
    except:
        driver.execute_script('window.stop()')
    ————————————————
    版权声明:本文为CSDN博主「cool_soup29」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/cool_soup29/article/details/88657643

    在执行自动化测试用例过程中,发现因为网络慢或其他原因导致driver.get(url) 时,页面一直在加载,页面没有加载完成就不会去继续执行下面的动作,但是实际上需要操作的元素已经加载出来了。

    解决方法

    第一步:使用 set_script_timeout() 设置等待最大时间。

    第二步:到了最大等待时间后页面如果仍然没有加载完成,执行js代码, driver.execute_script("window.stop()") 停止页面加载,执行下面的自动化测试步骤。
    代码如下:

    driver = self.driver
            # 设置页面最大加载时间
            driver.set_page_load_timeout(10)
            try:
                driver.get(self.base_url)
            except TimeoutException:
                print '!!!!!!time out after 10 seconds when loading page!!!!!!'
                # 当页面加载时间超过设定时间,通过js来stop,即可执行后续动作
                driver.execute_script("window.stop()")
    1. 设置了最大等待时间为10秒。
    2. 如果10秒没有加载完成,打印“time out after 10 seconds when loading page!”,然后停止加载,直接执行下面的测试步骤。
      # -*- coding: utf-8 -*-
      # @Time    : 2018/8/6 11:12
      # @Author  : 银河以北
      # @Email   : smilegks@163.com
      # @Introduction   : XXX
      
      def page_loading_timeout(driver, url, time):
          '''
          :param driver: 参数1,传入浏览器对象
          :param url: 参数2,传入url
          :param time: 参数3,设置超时时间,单位是秒
          :return:
          '''
          driver.set_page_load_timeout(time)
          try:
              driver.get(url)
          except:
              print "!!!!!!time out after %s seconds when loading page!!!!!!" % time
              # 当页面加载时间超过设定时间,通过js来stop,即可执行后续动作
              driver.execute_script("window.stop()")
  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/stvadv/p/11653381.html
Copyright © 2011-2022 走看看