gpt4 book ai didi

java - 有没有办法在我的测试自动化套件目录中自动更新 chromedriver?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:01 24 4
gpt4 key购买 nike

我有一个使用 Selenium 和 Python 的自动化框架。为了运行 Chrome 浏览器,我将 chrome 驱动程序放在我的自动化框架中的一个文件夹中。现在的问题是 chrome 驱动程序在一段时间后被更新,我的脚本因此开始失败,每次谷歌更新时我都需要将更新的 chrome 驱动程序放在目录中。

有没有我可以实现的自动化方法来解决这个问题?

最佳答案

我遇到了同样的问题,所以我制作了一个 selenium 脚本来自动从 chromedrivers 站点下载 exe 文件,并让我的所有脚本引用 chromedriver.exe 文件在我的 python 目录中的当前位置。我在调度程序中与谷歌更新并行运行这个脚本,这样当 chrome 更新时,驱动程序也会更新。

请随意使用它/改造它。

-- 获取文件属性.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

-- ChromeVersion.py --

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 --

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

关于java - 有没有办法在我的测试自动化套件目录中自动更新 chromedriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50602355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com