相信做过UI自动化的朋友们,都会碰到一个问题,那就是经常莫名其妙的自动化框架跑不起来了,然后一查发现是浏览器更新了,对应的webdriver驱动不匹配了,然后就得自己去看浏览器版本,然后去对应浏览器的webdriver网站下载匹配版本,然后再放到环境变量里,很麻烦。
那有没有什么方法让框架自己每次去检查浏览器版本,然后发现版本不匹配时,自己去下载对应驱动,然后安装呢,还真有,废话少说,直接上干货。
1.下载依赖包
pip install webdriver-manager
2.封装获取webdriver的方法
笔者这里封装的方法适合UI自动化框架使用,如果只是单纯写脚本不用这么复杂,封装的方法支持自动下载更新任意浏览器(比如谷歌、eage等)的webdriver,并且支持启动webdriver失败时,自动重试机制(因为经测试发现这个依赖包经常容易第一次启动失败)。
python
import os
import shutil
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from common.base_log import logger
class GetDriver:
"""
根据driver_type,自动安装下载浏览器驱动,并返回driver
:return:driver
"""
def __init__(self, driver_type: str):
# 判断drivers_type是不是字符串,再判断driver_type的浏览器类型是否符合指定类型
if isinstance(driver_type, str) and (
driver_type.lower() in ["chrome", "chrome-headless", "firefox", "firefox-headless", "edge"]):
self.driver_type = driver_type.lower()
else:
logger.error(f"不支持该浏览器类型:{driver_type}")
raise TypeError(f"不支持该浏览器类型:{driver_type}")
def get_driver(self):
"""启动 driver,失败时清理缓存后重试一次"""
try:
return self._start_driver()
except Exception as first_exc:
logger.warning(f" 启动[{self.driver_type}]浏览器失败:{first_exc}")
logger.info("准备清理驱动缓存后重试启动...")
self._clear_webdriver_cache()
try:
logger.info(f"开始尝试重新启动[{self.driver_type}]...")
return self._start_driver()
except Exception as final_exc:
logger.error(f"[{self.driver_type}] 浏览器清理缓存后仍然启动失败:{final_exc}")
raise
def _start_driver(self):
"""根据driver_type初始化不同的浏览器驱动"""
match self.driver_type:
case "chrome":
return self._chrome_driver()
case "chrome-headless":
return self._chrome_headless_driver()
case "firefox":
return self._firefox_driver()
case "firefox-headless":
return self._firefox_headless_driver()
case "edge":
return self._edge_driver()
case _:
raise ValueError(f"不支持的浏览器类型:{self.driver_type}")
def _clear_webdriver_cache(self):
"""清理webdriver-manager下载的驱动缓存"""
cache_dir = os.path.expanduser("~/.wdm")
if os.path.exists(cache_dir):
logger.info(f"开始清理缓存目录:{cache_dir}")
try:
shutil.rmtree(os.path.expanduser("~/.wdm"), ignore_errors=True)
logger.info("清理 webdriver 缓存成功")
except Exception as e:
logger.warning(f"清理缓存失败:{e}")
else:
logger.info("缓存目录不存在,无需清理")
def _setup_driver(self, driver):
"""通用 driver 设置:最大化窗口 + 隐式等待 + 清理 cookies"""
driver.maximize_window()
driver.implicitly_wait(10)
driver.delete_all_cookies() # 清除浏览器所有缓存
def _chrome_driver(self):
# chrome浏览器
# selenium 3的写法
# driver = webdriver.Chrome(ChromeDriverManager().install())
# selenium 4的写法
options = webdriver.ChromeOptions()
# 去掉"chrome正受到自动化测试软件的控制"的提示条
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
self._setup_driver(driver)
return driver
def _chrome_headless_driver(self):
# chrome headless模式
# 参数说明,参考地址:https://github.com/GoogleChrome/chrome-launcher/blob/master/docs/chrome-flags-for-tools.md#--enable-automation
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument("--headless")
chrome_option.add_argument("--no-sandbox") # 注意:linux运行必须要有这个
chrome_option.add_argument("--window-size=1920x1080")
# 本地chrome浏览器
# selenium 3的写法
# driver = webdriver.Chrome(ChromeDriverManager().install())
# selenium 4的写法
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_option)
self._setup_driver(driver)
return driver
def _firefox_driver(self):
# firefox浏览器
# selenium 3
# driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
# selenium 4
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
self._setup_driver(driver)
return driver
def _firefox_headless_driver(self):
# firefox headless模式
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--headless")
firefox_options.add_argument("--disable-gpu")
# selenium 3
# driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
# selenium 4
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=firefox_options)
self._setup_driver(driver)
return driver
def _edge_driver(self):
# Edge浏览器
# selenium 3
# driver = webdriver.Edge(EdgeChromiumDriverManager().install())
# selenium 4
edge_options = webdriver.EdgeOptions()
edge_options.use_chromium = True
# 屏蔽inforbar
edge_options.add_experimental_option('useAutomationExtension', False)
edge_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()), options=edge_options)
self._setup_driver(driver)
return driver
if __name__ == '__main__':
driver = GetDriver("chrome").get_driver()
print(driver)
driver.get("https://www.baidu.com")