我正在尝试编写一些防御代码,以防止有人安装旧版本的 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 绑定中没有方法来获取 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]
看来
moz:geckodriverVersion
已于 2018 年末的某个时候添加到功能中。
driverversion = driver.capabilities['moz:geckodriverVersion']
browserversion = driver.capabilities['browserVersion']
在 SUSE Linux 上,我只需输入 geckodriver --version。
回复是:
geckodriver 0.32.0 (4563dd583110 2022-10-13 09:22 +0000) 该程序的源代码可从 https://hg.mozilla.org/mozilla-central 中的testing/geckodriver。 本程序受 Mozilla 公共许可证 2.0 条款的约束。 您可以在 https://mozilla.org/MPL/2.0/ 获取许可证副本。