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)簽頁可用?。?/p>
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)文章
詳解Python之Scrapy爬蟲教程NBA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫
這篇文章主要介紹了詳解Python之Scrapy爬蟲教程NBA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01基于python OpenCV實現(xiàn)動態(tài)人臉檢測
這篇文章主要為大家詳細介紹了基于python OpenCV實現(xiàn)動態(tài)人臉檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05腳本測試postman快速導(dǎo)出python接口測試過程示例
這篇文章主要介紹了關(guān)于腳本測試postman快速導(dǎo)出python接口測試示例的過程操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python中reduce()函數(shù)的語法參數(shù)與作用詳解
這篇文章主要介紹了Python中reduce()函數(shù)的語法參數(shù)與作用詳解,reduce函數(shù)是通過函數(shù)對迭代器對象中的元素進行遍歷操作,Python3.x中reduce函數(shù)已經(jīng)從內(nèi)置函數(shù)中取消了,轉(zhuǎn)而放在functools模塊中,需要的朋友可以參考下2023-08-08