如何使用Python立即运行Chrome浏览器版本? [英] How can I get Chrome Browser Version running now with Python?

查看:766
本文介绍了如何使用Python立即运行Chrome浏览器版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒运行应用程序,并且我想知道实际安装的chrome浏览器版本,然后再运行Chrome驱动程序以避免出于兼容性原因出现任何异常.我知道我可以使用driver = webdriver.Chrome("path\\to\\chromedriver.exe")然后使用driver.capabilities['browserVersion']来显示版本,但是如果Chrome驱动程序版本与实际的chrome浏览器版本不同,则会引发异常.

I'm running a application using selenium, and I want to know actual chrome browser version installed, before running Chrome Driver to avoid any Exception for compatibility reason. I know I can use driver = webdriver.Chrome("path\\to\\chromedriver.exe") then driver.capabilities['browserVersion'] to show version but if Chrome Driver version differ from actual chrome browser version that's raise an exception.

谢谢

实际上,我为自己找到了答案,找到了解决方案:

Edited: Actually I found the answer for myself, the solution I found:

from win32com.client import Dispatch

def get_version_via_com(filename):
    parser = Dispatch("Scripting.FileSystemObject")
    try:
        version = parser.GetFileVersion(filename)
    except Exception:
        return None
    return version

if __name__ == "__main__":
    paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
             r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]
    version = list(filter(None, [get_version_via_com(p) for p in paths]))[0]
    print(version)
    # result: 80.0.3987.122

PS:我想人们一开始就听不懂我的问题,对不起我的英语

PS: I think people don't understand my question at the beginning and I'm sorry for my english

推荐答案

如果您正在使用硒,则可以使用driver.capabilities获取 chrome 浏览器版本.字典.

If you are using selenium, then you can get the chrome browser version using the driver.capabilities dictionary.

driver.capabilities['browserVersion']

较早版本的chromedriver存储了chrome浏览器版本driver.capabilities['version'].如果您想要获得chrome浏览器版本而不必担心这一点,则可以使用以下代码.

Earlier version of chromedriver stored the chrome browser version driver.capabilities['version']. If you want to get chrome browser version without having to worry about this, you can use the below code.

if 'browserVersion' in driver.capabilities:
    print(driver.capabilities['browserVersion'])
else:
    print(driver.capabilities['version'])

这篇关于如何使用Python立即运行Chrome浏览器版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆