对我有效的命令是:
pip install chromedriver-binary==your_chrome_version
如果您在自动集群上运行程序时遇到此错误,并且每次下载的是Google Chrome的稳定版本,则可以使用以下shell脚本动态下载兼容版本的Chrome驱动程序,即使Chrome的稳定版本已更新。
%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驱动程序,并且它完美地运行而没有任何问题。
希望能以某种方式帮助其他人。
有特定于驱动程序的NuGet包,可以将驱动程序二进制文件复制到您的 bin 文件夹中。例如,在我的 .NET Core 项目中,我使用来自此处的最新稳定 NuGet https://www.nuget.org/packages/Selenium.WebDriver.ChromeDriver/。
基本上,NuGet 包含 chromedriver.exe,并在每次复制到您的程序集文件夹中(在我的情况下是 \bin\Debug\netcoreapp3.1)。这是我在项目中使用的行:
"<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="*" />"
你可以使用以下方式添加:
dotnet add package Selenium.WebDriver.ChromeDriver --version *
当我第一次添加此引用时,我看到了有关此特定包的警告,但在关闭并重新打开我的Visual Studio 2019后,它消失了。
星号表示将使用最新(最高数字)稳定的软件包,如 此处 所述。
当然,您可以手动安装最新版本(不使用通配符,而是选择特定版本),然后在每个2个主要Chrome版本上更新NuGet软件包引用,以使其与您的Chrome版本匹配。(87 chromedriver适用于87和88 Chrome,但不适用于89)。
然后,使用以下行来实例化驱动程序:
var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
这种方法消除了需要指定具体路径到chromedriver.exe的需求,因为它将不得不更新。
Reference:
https://www.automatetheplanet.com/webdriver-net50/
<package id="Selenium.WebDriver.ChromeDriver" version="89.0.4389.2300" targetFramework="net45" />
更新你的NuGet包到适当的版本并解决问题。
您可以处理此异常
from selenium.common.exceptions import SessionNotCreatedException
添加此功能
def update_chrome():
try:
print("Updating chrome driver")
download_chrome = "wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"
unpack_chrome = "sudo dpkg -i google-chrome-stable_current_amd64.deb"
install_chrome = "sudo apt-get -f install"
file_path = os.path.join(os.environ['HOME'], "google-chrome-stable_current_amd64.deb")
if os.path.isfile(file_path):
print("{} found\nremoving {}".format(file_path, file_path))
os.remove(file_path)
else:
print("File does not exist {}".format(file_path))
old_chrome_ver = os.popen("/usr/bin/google-chrome --version").read().strip('Google Chrome ').strip().split(".")[0]
print("Current Chrome version: {}".format(old_chrome_ver))
os.system(command=download_chrome)
sleep(2)
os.system(command=unpack_chrome)
sleep(5)
os.system(command=install_chrome)
crnt_chrome_ver = os.popen("/usr/bin/google-chrome --version").read().strip('Google Chrome ').strip().split(".")[0]
print("Current Chrome version: {}".format(crnt_chrome_ver))
if crnt_chrome_ver == old_chrome_ver:
print("Chrome version is same as before")
elif crnt_chrome_ver > old_chrome_ver:
print("Chrome version is updated from {} to {}".format(old_chrome_ver, crnt_chrome_ver))
return True
except Exception as e:
print("Error while updating chrome: {}".format(e))
return False
然后调用这个函数
try:
# Your code here
except SessionNotCreatedException as snce:
print("Session not created: {}".format(snce))
chrome_update_result = False
if recurrences == 0:
chrome_update_result = update_chrome()
if chrome_update_result:
recurrences += 1
print("Chrome updated successfully")
erisapedia_bot(state, download_dir)
return True
else:
print("Chrome update failed")
return False
我已经在与会话想要运行的相同端口上运行了本地服务器,这导致了错误。关闭该本地服务器解决了我的问题。
brew install --cask chromedriver