selenium切換標(biāo)簽頁解決get超時問題的完整代碼

從 gif 直觀地感受一下效果
我有大量 url 需要訪問,但是有些 url 會超時
為了避免超時,設(shè)置driver.set_page_load_timeout(3)限時3秒,一旦超時就會產(chǎn)生 TimeoutException
而且超時后標(biāo)簽頁就卡柱了,只能通過 driver.close()關(guān)閉
如果你只有一個標(biāo)簽頁,關(guān)閉就直接退出了,還得重啟
自然想到先保留一個備用的標(biāo)簽,原標(biāo)簽超時需要關(guān)閉的時候就切換過來,然后再關(guān)閉,并打開新標(biāo)簽,保證任何時候都有兩個標(biāo)簽頁可用??!
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() # 關(guān)閉卡住的標(biāo)簽
driver.switch_to.window(win) # 切換到備用標(biāo)簽
main_win = win # 切換到備用標(biāo)簽
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()
總結(jié)
到此這篇關(guān)于selenium切換標(biāo)簽頁解決get超時問題的文章就介紹到這了,更多相關(guān)selenium切換標(biāo)簽頁解決get超時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何基于opencv實現(xiàn)簡單的數(shù)字識別
現(xiàn)在很多場景需要使用的數(shù)字識別,比如銀行卡識別,以及車牌識別等,在AI領(lǐng)域有很多圖像識別算法,大多是居于opencv 或者谷歌開源的tesseract 識別,下面這篇文章主要給大家介紹了關(guān)于如何基于opencv實現(xiàn)簡單的數(shù)字識別,需要的朋友可以參考下2021-09-09
Python用正則表達(dá)式實現(xiàn)爬取古詩文網(wǎng)站信息
這篇文章主要給大家介紹了關(guān)于Python如何利用正則表達(dá)式爬取爬取古詩文網(wǎng)站信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
python json.dumps() json.dump()的區(qū)別詳解
這篇文章主要介紹了python json.dumps() json.dump()的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
對Django中static(靜態(tài))文件詳解以及{% static %}標(biāo)簽的使用方法
今天小編就為大家分享一篇對Django中static(靜態(tài))文件詳解以及{% static %}標(biāo)簽的使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python數(shù)據(jù)庫PooledDB連接池初始化使用示例
這篇文章主要為大家介紹了python數(shù)據(jù)庫PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
python機器學(xué)習(xí)MATLAB最小二乘法的兩種解讀
這篇文章主要為大家介紹了python機器學(xué)習(xí)中MATLAB最小二乘法的兩種解讀方式,有需要的朋友可以借鑒參考下希望能夠有所幫助2022-02-02

