Python 在Mac上启动电子应用程序后,selenium web驱动程序挂起

Python 在Mac上启动电子应用程序后,selenium web驱动程序挂起,python,selenium,electron,Python,Selenium,Electron,我有一个新任务,在Mac上实现电子应用程序的自动化。因此,我使用python和selenium来启动任务 通过流畅的代码,我可以启动应用程序,但在应用程序启动后,自动化脚本挂起,甚至无法打印一些消息 这是我现在的代码: from time import sleep from selenium import webdriver def startdriver(): print("start service") service = webdriver.chrome.servic

我有一个新任务,在Mac上实现电子应用程序的自动化。因此,我使用python和selenium来启动任务

通过流畅的代码,我可以启动应用程序,但在应用程序启动后,自动化脚本挂起,甚至无法打印一些消息

这是我现在的代码:

from time import sleep

from selenium import webdriver

def startdriver():

    print("start service")
    service = webdriver.chrome.service.Service("chromedriver")
    service.start()
    print("started")
    print(service.service_url)
    caps = {
                               'browserName': 'chrome',
                               'goog:chromeOptions': {
                                   'args': ['--no-sandbox',
                                            '--disable-dev-shm-usage'],
                                   'binary': r"/Applications/Enterprise.app/Contents/MacOS/Enterprise",
                                   'extensions': [],
                                   'windowTypes': ['webview']},
                               'platform': 'ANY',
                               'version': ''}
    print(caps)
    driver = webdriver.remote.webdriver.WebDriver(
                 command_executor=service.service_url,
                 desired_capabilities=caps,

                 #desired_capabilities={'chromeOptions': caps},
                 browser_profile=None,
                 proxy=None,
                 keep_alive=False)
    sleep(4)
    print("start")
    driver.get("http://www.google.com")

以下是脚本的输出:

start service
started
http://localhost:50506
{'browserName': 'chrome', 'goog:chromeOptions': {'args': ['--no-sandbox', '--disable-dev-shm-usage'], 'binary': '/Applications/Enterprise.app/Contents/MacOS/Enterprise', 'extensions': [], 'windowTypes': ['webview']}, 'platform': 'ANY', 'version': ''}
大约1分钟后,应用程序关闭,脚本显示:

Traceback (most recent call last):
  File "/Users/leo.wong/auto/Auto/try/try4.py", line 31, in <module>
    keep_alive=False)
  File "/Users/leo.wong/auto/Auto/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Users/leo.wong/auto/Auto/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/leo.wong/auto/Auto/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/leo.wong/auto/Auto/venv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
回溯(最近一次呼叫最后一次):
文件“/Users/leo.wong/auto/auto/try/try4.py”,第31行,在
保持_活动(错误)
文件“/Users/leo.wong/auto/auto/venv/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,第157行,在__
启动会话(功能、浏览器配置文件)
文件“/Users/leo.wong/auto/auto/venv/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,第252行,在启动会话中
响应=self.execute(Command.NEW_会话,参数)
文件“/Users/leo.wong/auto/auto/venv/lib/python3.7/site packages/selenium/webdriver/remote/webdriver.py”,执行中第321行
self.error\u handler.check\u响应(响应)
文件“/Users/leo.wong/auto/auto/venv/lib/python3.7/site packages/selenium/webdriver/remote/errorhandler.py”,第242行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.WebDriverException:消息:未知错误:DevToolsActivePort文件不存在
我用电子官方网站上提到的Spectron在Java和Javascript中尝试过类似的代码。但我也看到了同样的事情

我的环境:

  • MacOS:10.14.6
  • Python:3.7
  • Chrome:79.0.3945.88版(官方版本)(64位)
  • Chromedriver:Chromedriver 79.0.3945.36

任何人都知道我如何让它工作,我想提供更多信息,但请让我知道我应该提供什么。

此异常可能有多种原因。其中一个最常见的问题是ChromeDriver版本与您尝试自动化的electron应用程序所使用的electron版本不兼容

例如,如果我的应用程序的电子版为v7.1.2,则使用的ChromeDriver版本应为78.0.3904.113

有关此兼容性的更多信息,请参见此处-

我尝试使用Java v1.8、Electron v7.1.6(在node_module/../dist文件夹中创建的默认Electron.exe)和ChromeDriver v78.0.3904.105的一个示例

ChromeOptions options = new ChromeOptions();
options.setBinary("./electron-quick-start/node_modules/electron/dist/electron.exe");
ChromeDriverService chromeservices = new ChromeDriverService.Builder().build();
WebDriver driver = new ChromeDriver(chromeservices, options);
String chromeVersion = driver.findElement(By.cssSelector("ul > li.chrome-version")).getText();
String electronVersion = driver.findElement(By.cssSelector("ul > li.electron-version")).getText();
System.out.println("Chromium version : " + chromeVersion);
System.out.println("Electron version : " + electronVersion);
driver.quit();

谢谢你的回答,最后我发现开发人员使用3.x来构建应用程序,而且肯定它与Chrome79.x不兼容。我将检查列表并切换回旧版本,希望这能解决我的问题。感谢这个想法,在我安装了与electron 3.x兼容的Spectron 5.0.0之后,我的脚本正在运行。