背景
- 很多瀏覽器會自動更新,但是 driver 不會自動更新。為了確保 driver 版本和瀏覽器匹配,可以使用第三方庫 webdriver_manager
代碼
- 這個(gè)文件里封裝了幾個(gè)函數(shù)
- driver_seek : 根據(jù)給定的目錄,和文件名稱,查找該目錄下是否有這個(gè)文件
- driver_download : 下載 webdriver 到指定目錄,如果path參數(shù)不指定,會下載到 C:\Users\當(dāng)前用戶名 目錄下,如果指定path,就下載到指定目錄 —— 我因?yàn)橛卸鄠€(gè)使用Selenium 的爬蟲項(xiàng)目,所以會把 driver 下載到1個(gè)公用目錄下
- driver_test : 測試上面這兩個(gè)函數(shù)是否工作正常
# !/usr/bin/env python3
# _*_ coding:utf-8 _*_
"""
@File : init_webdriver.py
@Project : Scrapy
@CreateTime : 2023/1/2 17:02
@Author : biaobro
@Software : PyCharm
@Last Modify Time : 2023/1/2 17:02
@Version : 1.0
@Description : None
@20230207
# 安裝webdriver-manager : pip install webdriver-manager
# webdriver-manager 在python3.11 下報(bào)錯(cuò)無法使用,改用python3.10 后正常
# 這個(gè)文件只需要完成 檢測目標(biāo)是否存在,如果不存在就下載的任務(wù)就好了,不需要做額外的設(shè)置,應(yīng)用層的設(shè)置交給應(yīng)用層
# 如果不寫main 函數(shù),被導(dǎo)入時(shí)就會自動執(zhí)行
"""
from selenium import webdriver
# chrome, firefox, edge, IE
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
import os
# from webdriver_manager.firefox import GeckoDriverManager
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
# from webdriver_manager.microsoft import IEDriverManager
def driver_seek(folder_name, file_name):
"""根據(jù)輸入的文件名稱查找對應(yīng)的文件夾有無改文件,有則返回文件路徑"""
for root, dirs, files in os.walk(folder_name):
if file_name in files:
# 當(dāng)層文件內(nèi)有該文件,輸出文件地址
path = os.path.join(root, file_name) # r'{0}\{1}'.format(root, file_name)
print(path)
print('the driver has already been there. you could load it freely.')
return path
print(file_name + " doesn't exist in " + folder_name + ', please download it firstly.')
return None
def driver_download(path=None):
try:
# 默認(rèn) webdriver 會被下載到 .home/.wdm folder
# 本機(jī) [C:\Users\biaob\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe]
# silent logs and remove them from console
# os.environ['WDM_LOG_LEVEL'] = '0'
# disable the blank space in first line
# os.environ['WDM_PRINT_FIRST_LINE'] = 'False'
# 如果沒有指定path參數(shù),就下載到項(xiàng)目路徑
# 如果指定,就下載到指定路徑
if path is None:
# By default, all driver binaries are saved to user.home/.wdm folder.
# You can override this setting and save binaries to project.root/.wdm.
# 設(shè)置 'WDM_LOCAL' = '1' 修改設(shè)置,下載文件到項(xiàng)目路徑
os.environ['WDM_LOCAL'] = '1'
# 下載地址:https://chromedriver.chromium.org/downloads
# 老式寫法
# browser = webdriver.Chrome(executable_path=ChromeDriverManager().install()) # , options=options)
# 新式寫法
print("driver will be downloaded into default project folder.")
ChromeService(ChromeDriverManager().install())
else:
# Set the directory where you want to download and save the webdriver.
# You can use relative and absolute paths.
print("driver will be downloaded into specified folder.")
path = ChromeDriverManager().install()
print(path)
return True
except Exception as e:
print(e)
return False
def driver_test(path, url="https://www.baidu.com"):
driver_path = driver_init()
# option set to avoid 'data' show in address bar
options = webdriver.ChromeOptions()
# options.add_argument('--no-sandbox')
# options.add_argument('--disable-dev-shm-usage')
# options.add_argument(r"user-data-dir=data")
# options.add_argument(r"headless")
browser = webdriver.Chrome(service=ChromeService(path), options=options)
browser.get(url)
if browser.title == "百度一下,你就知道":
print("selenium browser headless mode visit Baidu test passed.")
elif browser.title is not None:
print(f"selenium browser headless mode visit {url} test passed.")
else:
print("selenium browser headless mode test failed!")
# quit 必須要有,否則停留后臺,需要在任務(wù)管理器中手動關(guān)閉
browser.quit()
# specify the path directly
# 指定本地目錄
# driver = webdriver.Chrome('D:\Download\chromedriver_win32\chromedriver.exe')
# 用法示例
def driver_init():
parent_directory = r'..\\'
file_name = r'chromedriver.exe'
driver_path = driver_seek(parent_directory, file_name)
if driver_path is not None:
return driver_path
else:
driver_download(parent_directory)
return driver_seek(parent_directory, file_name)
運(yùn)行效果
文章來源:http://www.zghlxwxcb.cn/news/detail-735546.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-735546.html
到了這里,關(guān)于Selenium - 自動下載 webdriver的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!