如何在Chrome浏览器通过PythonSelenium自动更新时使用ChromeDriver的特定版本

2024-09-26 17:39:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Selenium的新来者,我现在可以用Selenium和Chromedriver一起做基本的自动测试,代码可以正常工作,但问题是Chrome浏览器总是在后端自动更新,而代码在Chrome更新后总是无法运行。我知道我需要下载新的chromedriver来解决这个问题,但是我想知道有没有什么方法可以在不禁用chromebrowser更新的情况下解决这个问题?tks公司。在

我使用的是Windows10/Chrome67/Python3.6.4/Selenium 3.12.0


Tags: 方法代码selenium情况浏览器公司chromechromedriver
2条回答

没有,除了更新ChromeDriver二进制版本之外,没有其他选择,而Chrome浏览器会不断自动更新。在

原因

每个Chrome浏览器都是在对现有功能进行某些添加、修改和删除后发布的。为了符合当前浏览器的一系列功能,Chrome团队将适时发布兼容的ChromeDriver二进制文件。这些ChromeDriver二进制文件能够与Chrome浏览器进行交互。ChromeDriver二进制文件的某些版本支持特定范围的Chrome Browser版本(一些最新版本),如下所示:

  • 铬驱动v80.0.3987.16(2019-12-19)

    Supports Chrome version 80
    
  • <36.39英里/小时

    Supports Chrome version 79
    
  • 铬驱动v78.0.3904.70(2019-10-21)

    Supports Chrome version 78
    
  • 铬驱动v77.0.3865.40(2019-08-20)

    Supports Chrome version 77
    
  • 铬驱动v76.0.3809.126(2019-08-20)

    Supports Chrome version 76
    
  • 铬驱动v75.0.3770.8(2019-04-29)

    Supports Chrome version 75
    
  • <14.03>

    Supports Chrome version 74
    
  • 铬驱动v73.0.3683.68(2019-03-06)

    Supports Chrome version 73
    
  • 铬驱动v2.46(2019-02-01)

    Supports Chrome v71-73
    
  • ChromeDriver v2.45(2018-12-10)

    Supports Chrome v70-72
    
  • ChromeDriver v2.44(2018-11-19)

    Supports Chrome v69-71
    
  • ChromeDriver v2.43(2018-10-16)

    Supports Chrome v69-71
    
  • ChromeDriver v2.42(2018-09-13)

    Supports Chrome v68-70
    
  • 铬驱动v2.41(2018-07-27)

    Supports Chrome v67-69
    
  • ChromeDriver v2.40(2018-06-07)

    Supports Chrome v66-68
    
  • ChromeDriver v2.39(2018-05-30)

    Supports Chrome v66-68
    
  • ChromeDriver v2.38(2018-04-17)

    Supports Chrome v65-67
    
  • ChromeDriver v2.37(2018-03-16)

    Supports Chrome v64-66
    
  • ChromeDriver v2.36(2018-03-02)

    Supports Chrome v63-65
    
  • ChromeDriver v2.35(2018-01-10)

    Supports Chrome v62-64
    
  • ChromeDriver v2.34(2017-12-10)

    Supports Chrome v61-63
    
  • ChromeDriver v2.33(2017-10-03)

    Supports Chrome v60-62
    
  • ChromeDriver v2.32(2017-08-30)

    Supports Chrome v59-61
    
  • ChromeDriver v2.31(2017-07-21)

    Supports Chrome v58-60
    
  • ChromeDriver v2.30(2017-06-07)

    Supports Chrome v58-60
    
  • ChromeDriver v2.29(2017-04-04)

    Supports Chrome v56-58
    

结论

要使脚本/程序与更新的Chrome浏览器保持交互,必须根据兼容性使ChromeDriver二进制版本与Chrome浏览器保持同步。在

这就是我构建的(也使用了另一个stackoverflow线程的一些预写代码),它可以为您工作。我每次都将脚本设置为从全局驱动程序脚本运行,以确保它使用正确的ChromeDriver.exe文件。在

但是在遇到此问题之前,您需要首先确保您正在安装新的驱动程序,这些脚本将自动下载最新版本/查找ChromeDriver的最新版本并将其下载到新的文件夹位置。只有当你的Chrome版本更新后,它才会使用新的文件夹位置。如果浏览器版本的chrome更新,并且在上没有可用版本,脚本应该会正常失败chromedriver.storage.谷歌皮斯.com。在

我在我的操作系统路径中设置了四个脚本,以便可以全局访问我的驱动程序。下面是我用来更新浏览器的脚本。在

希望这有道理。在

干杯! 马特

在获取文件属性.py在

# as per https://stackoverflow.com/questions/580924/python-windows-file-version-attribute

import win32api

#==============================================================================
def getFileProperties(fname):
#==============================================================================
    """
    Read all properties of the given file return them as a dictionary.
    """
    propNames = ('Comments', 'InternalName', 'ProductName',
        'CompanyName', 'LegalCopyright', 'ProductVersion',
        'FileDescription', 'LegalTrademarks', 'PrivateBuild',
        'FileVersion', 'OriginalFilename', 'SpecialBuild')

    props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}

    try:
        # backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc
        fixedInfo = win32api.GetFileVersionInfo(fname, '\\')
        props['FixedFileInfo'] = fixedInfo
        props['FileVersion'] = "%d.%d.%d.%d" % (fixedInfo['FileVersionMS'] / 65536,
                fixedInfo['FileVersionMS'] % 65536, fixedInfo['FileVersionLS'] / 65536,
                fixedInfo['FileVersionLS'] % 65536)

        # \VarFileInfo\Translation returns list of available (language, codepage)
        # pairs that can be used to retreive string info. We are using only the first pair.
        lang, codepage = win32api.GetFileVersionInfo(fname, '\\VarFileInfo\\Translation')[0]

        # any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle
        # two are language/codepage pair returned from above

        strInfo = {}
        for propName in propNames:
            strInfoPath = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName)
            ## print str_info
            strInfo[propName] = win32api.GetFileVersionInfo(fname, strInfoPath)

        props['StringFileInfo'] = strInfo
    except:
        pass

    return props

在镀铬.py在

^{pr2}$

中华人民共和国romeDriverAutomation.py在

from ChromeVersion import chrome_browser_version, nextVersion, lastVersion


driverName = "\\chromedriver.exe"

# defining base file directory of chrome drivers
driver_loc = #"C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37-32\\ChromeDriver\\"   ENTER the file path of your exe
#   I created a separate folder to house the versions of chromedriver, previous versions will be deleted after downloading the newest version.
# ie. version 75 will be deleted after 77 has been downloaded.

# defining the file path of your exe file automatically updating based on your browsers current version of chrome.
currentPath = driver_loc + chrome_browser_version + driverName 
# check file directories to see if chrome drivers exist in nextVersion


import os.path

# check if new version of drive exists  > only continue if it doesn't
Newpath = driver_loc + nextVersion

# check if we have already downloaded the newest version of the browser, ie if we have version 76, and have already downloaded a version of 77, we don't need to run any more of the script.
newfileloc = Newpath + driverName
exists = os.path.exists(newfileloc)


if (exists == False):

    #open chrome driver and attempt to download new chrome driver exe file.

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    import time
    chrome_options = Options()
    executable_path = currentPath
    driver = webdriver.Chrome(executable_path=executable_path, options=chrome_options)

    # opening up url of chromedriver to get new version of chromedriver.
    chromeDriverURL = 'https://chromedriver.storage.googleapis.com/index.html?path=' + nextVersion 

    driver.get(chromeDriverURL)

    time.sleep(5)
    # find records of table rows
    table = driver.find_elements_by_css_selector('tr')


    # check the length of the table
    Table_len = len(table)

    # ensure that table length is greater than 4, else fail.   table length of 4 is default when there are no availble updates
    if (Table_len > 4 ):

        # define string value of link
        rowText = table[(len(table)-2)].text[:6]
        time.sleep(1)
        # select the value of the row
        driver.find_element_by_xpath('//*[contains(text(),' + '"' + str(rowText) + '"'+')]').click()
        time.sleep(1)
        #select chromedriver zip for windows 
        driver.find_element_by_xpath('//*[contains(text(),' + '"' + "win32" + '"'+')]').click()

        time.sleep(3)
        driver.quit()

        from zipfile import ZipFile
        import shutil


        fileName = #r"C:\Users\Administrator\Downloads\chromedriver_win32.zip"  > enter your download path here.




        # Create a ZipFile Object and load sample.zip in it
        with ZipFile(fileName, 'r') as zipObj:
           # Extract all the contents of zip file in different directory
           zipObj.extractall(Newpath)


        # delete downloaded file
        os.remove(fileName)



        # defining old chrome driver location
        oldPath = driver_loc + lastVersion
        oldpathexists = os.path.exists(oldPath)

        # this deletes the old folder with the older version of chromedriver in it (version 75, once 77 has been downloaded)
        if(oldpathexists == True):
            shutil.rmtree(oldPath, ignore_errors=True)



exit()

https://github.com/MattWaller/ChromeDriverAutoUpdate

相关问题 更多 >

    热门问题