我开发了一个小型应用程序,可以自动执行 Intranet 上的某些任务。为了方便与没有 Python 的用户共享,我将此应用程序导出为可执行文件 (.exe)。该应用程序已经测试了一段时间并且运行正常。
但是,浏览器更新时会出现问题:由于 WebDriver 的版本与默认的 Microsoft Edge 浏览器版本不匹配,导致脚本无法使用。当我尝试通过 IDE 启动脚本时,显示以下错误消息:
>
> > ```
> SessionNotCreatedException: session not created: This version of Microsoft Edge WebDriver only supports Microsoft Edge version 124
> Current browser version is 123.0.2420.81 with binary path C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
> > ```
有没有办法自动更新WebDriver,使其与当前浏览器版本匹配?我必须使用 Microsoft Edge 来完成这个项目。
预先感谢您提供的任何帮助或建议。
从 v4.6 开始,Selenium 现在包含一个内置驱动程序管理器,Selenium Manager。您的起始代码可以很简单
from selenium import webdriver
url = "about:blank"
driver = webdriver.Edge()
driver.maximize_window()
driver.get(url)
# do stuff
Selenium 将自动下载匹配的浏览器和版本,配置并在您的脚本中使用它。
运行您建议的代码时,我遇到以下错误消息:
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\driver_finder.py:38
> in get_path
> path = SeleniumManager().driver_location(options) if path is None else path
>
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:106
> in driver_location
> output = self.run(args)
>
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:154
> in run
> raise WebDriverException(f"Unsuccessful command executed: {command}.\n{result}{stderr}")
>
> WebDriverException: Unsuccessful command executed:
> C:\Users\nx0613630.AIRCELLECORP\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe
> --browser MicrosoftEdge --language-binding python --output json. {'code': 65, 'message': 'error sending request for url
> (https://msedgedriver.azureedge.net/LATEST_RELEASE_123_WINDOWS): error
> trying to connect: dns error: Hôte inconnu. (os error 11001)',
> 'driver_path': '', 'browser_path': ''}
>
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
>
> Cell In[8], line 4
> driver = webdriver.Edge()
>
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\edge\webdriver.py:45
> in __init__
> super().__init__(
>
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\chromium\webdriver.py:49
> in __init__
> self.service.path = DriverFinder.get_path(self.service, options)
>
> File
> ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\driver_finder.py:41
> in get_path
> raise NoSuchDriverException(msg) from err
>
> NoSuchDriverException: Unable to obtain driver for MicrosoftEdge using
> Selenium Manager.; For documentation on this error, please visit:
> https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
我认为发生此错误是因为 Selenium 无法从网站 https://msedgedriver.azureedge.net/LATEST_RELEASE_123_WINDOWS 自动下载 Microsoft Edge 驱动程序。在我看来,这是因为我需要通过代理(例如:pip --proxy http://vip2.cori.ade.cdm:8080 install selenium),我尝试使用IA建议解决问题但我有同样的问题:
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "http://vip2.cori.ade.cdm:8080"
proxy.ssl_proxy = "http://vip2.cori.ade.cdm:8080"
capabilities = webdriver.DesiredCapabilities.EDGE.copy()
proxy.add_to_capabilities(capabilities)
driver = webdriver.Edge(desired_capabilities=capabilities)
driver.get("https://www.google.com")
################################################## ###########
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "http://vip2.cori.ade.cdm:8080"
proxy.ssl_proxy = "http://vip2.cori.ade.cdm:8080"
capabilities = webdriver.DesiredCapabilities.EDGE
proxy.add_to_capabilities(capabilities)
driver = webdriver.Edge(desired_capabilities=capabilities)
driver.get("https://www.google.com")
################################################## ############
from selenium import webdriver
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument("--proxy-server=http://vip2.cori.ade.cdm:8080")
driver = webdriver.Edge(options=options)
driver.get("https://www.google.com")