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

所属分类: 脚本专栏 / python 阅读数: 470
收藏 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()

总结

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

python2.7无法使用pip的解决方法(安装easy_install)

下面小编就为大家分享一篇python2.7无法使用pip的解决方法(安装easy_install),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python实现的计算马氏距离算法示例

这篇文章主要介绍了Python实现的计算马氏距离算法,简单说明了马氏距离算法原理,并结合实例形式分析了Python实现与使用马氏距离算法的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python逐行读写txt文件的实例讲解

下面小编就为大家分享一篇python逐行读写txt文件的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python批量读取txt文件为DataFrame的方法

下面小编就为大家分享一篇python批量读取txt文件为DataFrame的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python通过调用mysql存储过程实现更新数据功能示例

这篇文章主要介绍了Python通过调用mysql存储过程实现更新数据功能,结合实例形式分析了Python调用mysql存储过程实现更新数据的具体步骤与相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现的HMacMD5加密算法示例

这篇文章主要介绍了Python实现的HMacMD5加密算法,简单说明了HMAC-MD5加密算法的概念、原理并结合实例形式分析了Python实现HMAC-MD5加密算法的相关操作技巧,,末尾还附带了Java实现HMAC-MD5加密算法的示例,需要的朋友可以参考下
收藏 0 赞 0 分享

图解Python变量与赋值

Python是一门独特的语言,与C语言有很大区别,初学Python很多萌新表示对变量与赋值不理解,这里就大家介绍一下,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的并发处理之asyncio包使用的详解

本篇文章主要介绍了Python中的并发处理之asyncio包使用的详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python获取二维矩阵每列最大值的方法

下面小编就为大家分享一篇Python获取二维矩阵每列最大值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy找出array中的最大值,最小值实例

下面小编就为大家分享一篇numpy找出array中的最大值,最小值实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多