How to work with a specific version of ChromeDriver while Chrome Browser gets updated automatically through Python selenium
我是Selenium的新手,现在可以将selenium与Chromedriver一起使用进行基本的自动测试,代码可以正常工作,但问题是Chrome浏览器始终在后端自动更新,并且代码始终在Chrome更新后无法运行 。 我知道我需要下载新的chromedriver来解决此问题,但是我想知道是否有任何方法可以在不禁用chromebrowser更新的情况下解决此问题? tks。
我正在使用Windows 10 / Chrome版本67 / Python 3.6.4 / Selenium 3.12.0
不,除了更新ChromeDriver二进制版本外,别无选择,而Chrome浏览器会不断自动更新。
原因
在添加,修改和删除现有功能后,会释放每个Chrome浏览器。为了符合当前的浏览器功能集,Chrome团队会不时发布兼容的ChromeDriver二进制文件。这些ChromeDriver二进制文件能够与Chrome浏览器进行交互。某些版本的ChromeDriver二进制文件支持特定范围的Chrome浏览器版本(某些是最新版本),如下所示:
-
ChromeDriver v81.0.4044.20(2020-02-13)
1Supports Chrome version 80 -
ChromeDriver v80.0.3987.106(2020-02-13)
1Supports Chrome version 80 -
ChromeDriver v79.0.3945.36(2019-11-18)
1Supports Chrome version 79 -
ChromeDriver v78.0.3904.70(2019-10-21)
1Supports Chrome version 78 -
ChromeDriver v77.0.3865.40(2019-08-20)
1Supports Chrome version 77 -
ChromeDriver v76.0.3809.126(2019-08-20)
1Supports Chrome version 76 -
ChromeDriver v75.0.3770.8(2019-04-29)
1Supports Chrome version 75 -
ChromeDriver v74.0.3729.6(2019-03-14)
1Supports Chrome version 74 -
ChromeDriver v73.0.3683.68(2019-03-06)
1Supports Chrome version 73 -
ChromeDriver v2.46(2019-02-01)
1Supports Chrome v71-73 -
ChromeDriver v2.45(2018-12-10)
1Supports Chrome v70-72 -
ChromeDriver v2.44(2018-11-19)
1Supports Chrome v69-71 -
ChromeDriver v2.43(2018-10-16)
1Supports Chrome v69-71 -
ChromeDriver v2.42(2018-09-13)
1Supports Chrome v68-70 -
ChromeDriver v2.41(2018-07-27)
1Supports Chrome v67-69 -
ChromeDriver v2.40(2018-06-07)
1Supports Chrome v66-68 -
ChromeDriver v2.39(2018-05-30)
1Supports Chrome v66-68 -
ChromeDriver v2.38(2018-04-17)
1Supports Chrome v65-67 -
ChromeDriver v2.37(2018-03-16)
1Supports Chrome v64-66 -
ChromeDriver v2.36(2018-03-02)
1Supports Chrome v63-65 -
ChromeDriver v2.35(2018-01-10)
1Supports Chrome v62-64 -
ChromeDriver v2.34(2017-12-10)
1Supports Chrome v61-63 -
ChromeDriver v2.33(2017-10-03)
1Supports Chrome v60-62 -
ChromeDriver v2.32(2017-08-30)
1Supports Chrome v59-61 -
ChromeDriver v2.31(2017-07-21)
1Supports Chrome v58-60 -
ChromeDriver v2.30(2017-06-07)
1Supports Chrome v58-60 -
ChromeDriver v2.29(2017-04-04)
1Supports Chrome v56-58
结论
要使脚本/程序与更新的Chrome浏览器保持交互,您必须根据兼容性将ChromeDriver二进制版本与Chrome浏览器保持同步。
您可以使用以下Shell脚本来确保下载了正确版本的chrome驱动程序。您可以在python中执行类似的操作以使其工作,但是您将了解如何继续解决该问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | %sh #downloading compatible chrome driver version #getting the current chrome browser version **chromeVersion=$(google-chrome --product-version)** #getting the major version value from the full version **chromeMajorVersion=${chromeVersion%%.*}** # setting the base url for getting the release url for the chrome driver **baseDriverLatestReleaseURL=https://chromedriver.storage.googleapis.com/LATEST_RELEASE_** #creating the latest release driver url based on the major version of the chrome **latestDriverReleaseURL=$baseDriverLatestReleaseURL$chromeMajorVersion** **echo $latestDriverReleaseURL** #file name of the file that gets downloaded which would contain the full version of the chrome driver to download **latestDriverVersionFileName="LATEST_RELEASE_"$chromeMajorVersion** #downloading the file that would contain the full release version compatible with the major release of the chrome browser version **wget $latestDriverReleaseURL** #reading the file to get the version of the chrome driver that we should download **latestFullDriverVersion=$(cat $latestDriverVersionFileName)** **echo $latestFullDriverVersion** #creating the final URL by passing the compatible version of the chrome driver that we should download **finalURL="https://chromedriver.storage.googleapis.com/"$latestFullDriverVersion"/chromedriver_linux64.zip"** **echo $finalURL** **wget $finalURL** |
在databricks环境上运行计划作业时,我可以使用上述方法获得chrome浏览器和chrome驱动程序的兼容版本,并且它像没有任何问题的魅力一样工作。
希望它以某种方式帮助他人。
是的你可以。
问题:"如何通过Python硒自动更新Chrome浏览器时如何使用特定版本的ChromeDriver"
就像您正确指出的那样,Chrome浏览器会自动更新。如果ChromeDriver是计算机上某个特定版本的Chrome浏览器的静态文件,则意味着每次浏览器更新时,您都必须下载新的ChromeDriver。
幸运的是,还有一种方法可以自动更新ChromeDriver!
您可以使用webdrive-manager自动使用正确的chromedriver。
安装webdrive-manager:
1 | pip install webdriver-manager |
然后如下所示在python中使用驱动程序
1 2 3 4 | from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) |
对于Ubuntu / Linux:
只需使用它即可更新到最新版本:https://stackoverflow.com/a/57306360/4240654
1 2 3 | version=$(curl -s https://chromedriver.storage.googleapis.com/LATEST_RELEASE) wget -qP"/tmp/""https://chromedriver.storage.googleapis.com/${version}/chromedriver_linux64.zip" sudo unzip -o /tmp/chromedriver_linux64.zip -d /usr/bin |
然后,如果您需要更新Chrome,请执行以下操作:https://superuser.com/questions/130260/how-to-update-google-chrome-in-ubuntu
1 | sudo apt-get --only-upgrade install google-chrome-stable |
这就是我构建的(也使用了另一个stackoverflow线程的一些预编写代码),它可以为您工作。我每次都将脚本设置为从全局驱动程序脚本运行,以确保其使用正确的ChromeDriver.exe文件。
但是,在遇到此问题之前,您首先需要确保安装新的驱动程序,这些脚本将自动下载最新版本/寻找最新版本的ChromeDriver并将其下载到新的文件夹位置。 Chrome版本更新后,它将仅使用新的文件夹位置。如果chrome的浏览器版本更新了,并且chromedriver.storage.googleapis.com上没有可用的版本,则脚本应正常运行。
我在操作系统路径中设置了四个脚本,因此可以全局访问驱动程序。以下是我用来更新浏览器的脚本。
希望这是有道理的。
干杯!
马特
-getFileProperties.py-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # 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 |
-ChromeVersion.py-
1 2 3 4 5 6 7 8 9 10 11 12 13 | from getFileProperties import * chrome_browser = #'C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe' -- ENTER YOUR Chrome.exe filepath cb_dictionary = getFileProperties(chrome_browser) # returns whole string of version (ie. 76.0.111) chrome_browser_version = cb_dictionary['FileVersion'][:2] # substring version to capabable version (ie. 77 / 76) nextVersion = str(int(chrome_browser_version) +1) # grabs the next version of the chrome browser lastVersion = str(int(chrome_browser_version) -1) # grabs the last version of the chrome browser |
-ChromeDriverAutomation.py-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 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