Chrome驱动程序更新早于 Chrome

ChromeDriver updated before Chrome did

所以我遇到了与所列问题完全相同的问题

在那个问题中,问题是 Chrome 驱动程序是版本 99,但 Chrome 是版本 98。接受的答案指出 Chrome 驱动程序适用于Chrome 那是“还没出来”。我目前正在使用此代码获取最新版本的 ChromeDriver

# get the latest chrome driver version number
chrome_url = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'
response = requests.get(chrome_url)
version_number = response.text

# build the donwload url
download_url = "https://chromedriver.storage.googleapis.com/" + version_number + "/chromedriver_win32.zip"

有没有办法用 return 当前版本的 chrome 替换代码中的第一部分?

我找到了一种让它工作的方法,可能不是最好的方法,但它确实有效。

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

if __name__ == "__main__":
    # Get Version of Chrome
    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]

    # build the donwload url
    download_url = "https://chromedriver.storage.googleapis.com/" + version + "/chromedriver_win32.zip"

    # download the zip file using the url built above
    latest_driver_zip = wget.download(download_url, 'chromedriver.zip')

    # extract the zip file
    with zipfile.ZipFile(latest_driver_zip, 'r') as zip_ref:
        zip_ref.extractall()  # you can specify the destination folder path here
    # delete the zip file downloaded above
    os.remove(latest_driver_zip)