从 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()
总结
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“selenium切换标签页解决get超时问题的完整代码”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]