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

從 gif 直觀地感受一下效果
我有大量 url 需要訪問,但是有些 url 會(huì)超時(shí)
為了避免超時(shí),設(shè)置driver.set_page_load_timeout(3)限時(shí)3秒,一旦超時(shí)就會(huì)產(chǎn)生 TimeoutException
而且超時(shí)后標(biāo)簽頁就卡柱了,只能通過 driver.close()關(guān)閉
如果你只有一個(gè)標(biāo)簽頁,關(guān)閉就直接退出了,還得重啟
自然想到先保留一個(gè)備用的標(biāo)簽,原標(biāo)簽超時(shí)需要關(guān)閉的時(shí)候就切換過來,然后再關(guān)閉,并打開新標(biāo)簽,保證任何時(shí)候都有兩個(gè)標(biāo)簽頁可用??!
def visit(urls, timeout=3):
driver.implicitly_wait(timeout) # 操作、獲取元素時(shí)的隱式等待時(shí)間
driver.set_page_load_timeout(timeout) # 頁面加載超時(shí)等待時(shí)間
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超時(shí)問題的文章就介紹到這了,更多相關(guān)selenium切換標(biāo)簽頁解決get超時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫
這篇文章主要介紹了詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
基于Python的接口自動(dòng)化讀寫excel文件的方法
這篇文章主要介紹了基于Python的接口自動(dòng)化讀寫excel文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
基于python OpenCV實(shí)現(xiàn)動(dòng)態(tài)人臉檢測(cè)
這篇文章主要為大家詳細(xì)介紹了基于python OpenCV實(shí)現(xiàn)動(dòng)態(tài)人臉檢測(cè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
腳本測(cè)試postman快速導(dǎo)出python接口測(cè)試過程示例
這篇文章主要介紹了關(guān)于腳本測(cè)試postman快速導(dǎo)出python接口測(cè)試示例的過程操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
Python中reduce()函數(shù)的語法參數(shù)與作用詳解
這篇文章主要介紹了Python中reduce()函數(shù)的語法參數(shù)與作用詳解,reduce函數(shù)是通過函數(shù)對(duì)迭代器對(duì)象中的元素進(jìn)行遍歷操作,Python3.x中reduce函數(shù)已經(jīng)從內(nèi)置函數(shù)中取消了,轉(zhuǎn)而放在functools模塊中,需要的朋友可以參考下2023-08-08
Python實(shí)現(xiàn)自動(dòng)整理表格的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)自動(dòng)整理表格的功能,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03

