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


Posted in Python onAugust 30, 2020

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

从 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()

总结

到此这篇关于selenium切换标签页解决get超时问题的文章就介绍到这了,更多相关selenium切换标签页解决get超时内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python中的多重继承实例讲解
Sep 28 Python
Python中转换角度为弧度的radians()方法
May 18 Python
利用python画出折线图
Jul 26 Python
Python3实现对列表按元组指定列进行排序的方法分析
Dec 22 Python
python 读取dicom文件,生成info.txt和raw文件的方法
Jan 24 Python
wxPython实现列表增删改查功能
Nov 19 Python
python 实现兔子生兔子示例
Nov 21 Python
python手写均值滤波
Feb 19 Python
Python环境下安装PyGame和PyOpenGL的方法
Mar 25 Python
为什么说python适合写爬虫
Jun 11 Python
python实现人性化显示金额数字实例详解
Sep 25 Python
Python eval函数原理及用法解析
Nov 14 Python
五分钟带你搞懂python 迭代器与生成器
Aug 30 #Python
python开根号实例讲解
Aug 30 #Python
python一些性能分析的技巧
Aug 30 #Python
python脚本第一行如何写
Aug 30 #Python
golang/python实现归并排序实例代码
Aug 30 #Python
python创建文本文件的简单方法
Aug 30 #Python
python 中的9个实用技巧,助你提高开发效率
Aug 30 #Python
You might like
搜索和替换文件或目录的一个好类--很实用
2006/10/09 PHP
php数组的概述及分类与声明代码演示
2013/02/26 PHP
php简单浏览目录内容的实现代码
2013/06/07 PHP
PHP扩展CURL的用法详解
2014/06/20 PHP
PHP+MySQL实现无极限分类栏目的方法
2015/12/23 PHP
Javascript编写2048小游戏
2015/07/07 Javascript
快速学习jQuery插件 jquery.validate.js表单验证插件使用方法
2015/12/01 Javascript
JS简单编号生成器实现方法(附demo源码下载)
2016/04/05 Javascript
深入理解node exports和module.exports区别
2016/06/01 Javascript
Vue Spa切换页面时更改标题的实例代码
2017/07/15 Javascript
js提取中文拼音首字母的封装工具类
2018/03/12 Javascript
JavaScript变量提升和严格模式实例分析
2019/01/27 Javascript
微信小程序如何实现五星评价功能
2019/10/15 Javascript
js实现删除json中指定的元素
2020/09/22 Javascript
Js利用正则表达式去除字符串的中括号
2020/11/23 Javascript
解决python "No module named pip" 的问题
2018/10/13 Python
在python 不同时区之间的差值与转换方法
2019/01/14 Python
Flask框架踩坑之ajax跨域请求实现
2019/02/22 Python
pytorch-神经网络拟合曲线实例
2020/01/15 Python
django 读取图片到页面实例
2020/03/27 Python
Hawes & Curtis官网:英国经典品牌
2019/07/27 全球购物
护士个人简历自荐信
2013/10/18 职场文书
《骆驼和羊》教学反思
2014/02/27 职场文书
中药学自荐信
2014/06/15 职场文书
中秋晚会活动方案
2014/08/31 职场文书
校园新闻广播稿5篇
2014/10/10 职场文书
2014年终个人工作总结
2014/11/07 职场文书
2014年业务员工作总结范文
2014/11/17 职场文书
2015年班级工作总结范文
2015/04/03 职场文书
2015年社会治安综合治理工作总结
2015/04/10 职场文书
家长通知书家长意见
2015/06/03 职场文书
2015年圣诞节寄语
2015/08/17 职场文书
2016高考寄语集锦
2015/12/04 职场文书
《女娲补天》读后感5篇
2019/12/31 职场文书
动画《新网球王子 U-17 WORLD CUP》希腊队PV公开
2022/04/02 日漫
Android 界面一键变灰 深色主题工具类
2022/04/28 Java/Android