selenium切换标签页解决get超时问题的完整代码

所属分类: 脚本专栏 / python 阅读数: 479
收藏 0 赞 0 分享

从 gif 直观地感受一下效果

我有大量 url 需要访问,但是有些 url 会超时

为了避免超时,设置driver.set_page_load_timeout(3)限时3秒,一旦超时就会产生 TimeoutException

而且超时后标签页就卡柱了,只能通过 driver.close()关闭

如果你只有一个标签页,关闭就直接退出了,还得重启

自然想到先保留一个备用的标签,原标签超时需要关闭的时候就切换过来,然后再关闭,并打开新标签,保证任何时候都有两个标签页可用!!

def visit(urls, timeout=3):
 driver.implicitly_wait(timeout) # 操作、获取元素时的隐式等待时间
 driver.set_page_load_timeout(timeout) # 页面加载超时等待时间
 
 main_win = driver.current_window_handle
 
 for url in urls:
  all_win = driver.window_handles
  try:
   if len(all_win) == 1:
    driver.execute_script('window.open();')
   driver.get(url)
   # 页面处理
   pass
   
  except Exception:
   for win in all_win:
    if main_win != win:
     driver.close() # 关闭卡住的标签
     driver.switch_to.window(win) # 切换到备用标签
     main_win = win # 切换到备用标签
     break

完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
import time
import requests
import zipfile
import os

def un_zip(file_name, to_dir='./'):
 """unzip zip file"""
 zip_file = zipfile.ZipFile(file_name)
 if os.path.isdir(to_dir):
  pass
 else:
  os.mkdir(to_dir)
 for names in zip_file.namelist():
  zip_file.extract(names, to_dir)
 zip_file.close()

 
def download_driver(to_dir='./', version=''):
 print('install chrome-driver first')
 url = 'http://npm.taobao.org/mirrors/chromedriver/LATEST_RELEASE'
 if len(version)>0:
  url = 'http://npm.taobao.org/mirrors/chromedriver/LATEST_RELEASE_'+version
  
 version = requests.get(url).content.decode('utf8')
 driver_file = 'http://npm.taobao.org/mirrors/chromedriver/' + version + '/chromedriver_win32.zip'
 r = requests.get(driver_file)
 download_zip = "chromedriver_win32.zip"
 with open(download_zip, "wb") as code:
  code.write(r.content)
 un_zip(download_zip, to_dir)
 os.remove(download_zip)


try:
 driver = webdriver.Chrome()
except Exception as e:
 download_driver(to_dir='./', version='76')
 driver = webdriver.Chrome()

with open("url.txt", 'r') as file:
 urls = [ line.strip('\n') for line in file.readlines()]

visit(urls)

for i in driver.window_handles:
 driver.switch_to.window(i)
 driver.close()

总结

更多精彩内容其他人还在看

Python简单操作sqlite3的方法示例

这篇文章主要介绍了Python简单操作sqlite3的方法,结合实例形式分析了Python针对sqlite3数据库的读取、创建、增删改查等基本操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现遍历目录的方法【测试可用】

这篇文章主要介绍了Python实现遍历目录的方法,涉及Python针对目录与文件的遍历、判断、读取相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python条件变量之生产者与消费者操作实例分析

这篇文章主要介绍了python条件变量之生产者与消费者操作,结合具体实例形式分析了Python条件变量的概念、原理、及线程操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

利用pyinstaller或virtualenv将python程序打包详解

这篇文章主要给大家介绍了利用pyinstaller将python程序打包的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python多线程经典问题之乘客做公交车算法实例

这篇文章主要介绍了Python多线程经典问题之乘客做公交车算法,简单描述了乘客坐公交车问题并结合实例形式分析了Python多线程实现乘客坐公交车算法的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python编程实现删除VC临时文件及Debug目录的方法

这篇文章主要介绍了Python编程实现删除VC临时文件及Debug目录的方法,涉及Python针对文件与目录的遍历、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python3中dict(字典)的使用方法示例

这篇文章主要介绍了python3中dict(字典)的使用方法,文中给出了详细的功能列举,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python中.py文件打包成exe可执行文件详解

这篇文章主要给大家介绍了在Python中.py文件打包成exe可执行文件的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python编程之event对象的用法实例分析

这篇文章主要介绍了Python编程之event对象的用法,结合实例形式分析了event对象在线程通信中的作用与使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Python爬取网页中的图片(搜狗图片)详解

没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。下面这篇文章主要给大家介绍了利用Python3.6爬取搜狗图片网页中图片的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享
查看更多