Skip to main content
 首页 » 编程设计

python之你如何从 python selenium firefox webdriver 获取 geckodriver 版本

2024年08月16日47xiaohuochai

我正在尝试编写一些防御性代码,以防止有人在安装了旧版本的 geckodriver 时执行脚本。我似乎一辈子都无法从 webdriver 对象中获取 geckodriver 版本。

我找到的最接近的是 driver.capabilities,它包含 firefox 浏览器版本,但不包含 geckodriver 版本。

from selenium import webdriver 
driver = webdriver.Firefox() 
pprint(driver.capabilities) 

输出:

{'acceptInsecureCerts': True, 
 'browserName': 'firefox', 
 'browserVersion': '60.0', 
 'moz:accessibilityChecks': False, 
 'moz:headless': False, 
 'moz:processID': 18584, 
 'moz:profile': '/var/folders/qz/0dsxssjd1133p_y44qbdszn00000gp/T/rust_mozprofile.GsKFWZ9kFgMT', 
 'moz:useNonSpecCompliantPointerOrigin': False, 
 'moz:webdriverClick': True, 
 'pageLoadStrategy': 'normal', 
 'platformName': 'darwin', 
 'platformVersion': '17.5.0', 
 'rotatable': False, 
 'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}} 

浏览器版本和 geckodriver 版本是否可以直接链接?如果没有,我如何从 python 中检查 geckodriver 版本?

请您参考如下方法:

python 绑定(bind)中没有获取 geckodriver 版本的方法,你必须自己实现它,我的第一个选择是 subprocess

# Mind the encoding, it must match your system's 
output = subprocess.run(['geckodriver', '-V'], stdout=subprocess.PIPE, encoding='utf-8') 
version = output.stdout.splitlines()[0].split()[-1]