无法创建会话。未知错误:Chrome未能开始:被杀。 (未知错误:DevToolSactivePort文件不存在)

发布于 2025-02-07 10:31:52 字数 2594 浏览 4 评论 0原文

我通过使用以下代码使用硒来执行电子测试:

    const webdriver = require('selenium-webdriver');
    var Application = require('spectron').Application;
    var assert = require('assert');
    
    const driver = new webdriver.Builder()
        // The "9515" is the port opened by chrome driver.
        .usingServer('http://localhost:9515')
        .withCapabilities({
            'goog:chromeOptions': {
                // Here is the path to your Electron binary.
                binary: 'C:/Program Files/Google/Chrome/Application/chrome.exe',
            },
        })
        .forBrowser('electron') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
        .build();
    
    describe('Application launch', function () {
        var app;
        jest.setTimeout(30000);
        beforeEach(function () {
            app = new Application({
                path: 'node_modules/.bin/electron',
                args: ['--headless', '--no-sandbox', '--disable-dev-shm-usage'],
            });
            return app.start();
        });
    
        afterEach(function () {
            if (app && app.isRunning()) {
                return app.stop();
            }
        });
    
        it('click a button', function* () {
            yield driver.sleep(5000);
        });
    });

但是我要获得以下错误:

yarn run v1.22.19
warning package.json: License should be a valid SPDX license expression
warning ..\..\..\package.json: No license field
$ jest --NODE_ENV=test
 FAIL  src/__test__/App.spec.jsx (10.623 s)
  Application launch
    × click a button (6564 ms)

  ● Application launch › click a button

    Failed to create session.
    unknown error: Chrome failed to start: was killed.
      (unknown error: DevToolsActivePort file doesn't exist)
      (The process started from chrome location C:\Users\vipul\appname\src\node_modules\spectron\lib\launcher.bat is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

      at startWebDriverSession (src/node_modules/webdriver/build/utils.js:72:15)
      at Function.newSession (src/node_modules/webdriver/build/index.js:58:45)
      at Object.remote (src/node_modules/webdriverio/build/index.js:73:22)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        11.878 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我尝试添加其他stackoverflow问题所建议的args,例如 - 无头或 - - 没有袋装,但即使是那些不起作用。

I'm executing an automated test with Electron by using Selenium by using the following code:

    const webdriver = require('selenium-webdriver');
    var Application = require('spectron').Application;
    var assert = require('assert');
    
    const driver = new webdriver.Builder()
        // The "9515" is the port opened by chrome driver.
        .usingServer('http://localhost:9515')
        .withCapabilities({
            'goog:chromeOptions': {
                // Here is the path to your Electron binary.
                binary: 'C:/Program Files/Google/Chrome/Application/chrome.exe',
            },
        })
        .forBrowser('electron') // note: use .forBrowser('electron') for selenium-webdriver <= 3.6.0
        .build();
    
    describe('Application launch', function () {
        var app;
        jest.setTimeout(30000);
        beforeEach(function () {
            app = new Application({
                path: 'node_modules/.bin/electron',
                args: ['--headless', '--no-sandbox', '--disable-dev-shm-usage'],
            });
            return app.start();
        });
    
        afterEach(function () {
            if (app && app.isRunning()) {
                return app.stop();
            }
        });
    
        it('click a button', function* () {
            yield driver.sleep(5000);
        });
    });

But I'm obtaining the following error:

yarn run v1.22.19
warning package.json: License should be a valid SPDX license expression
warning ..\..\..\package.json: No license field
$ jest --NODE_ENV=test
 FAIL  src/__test__/App.spec.jsx (10.623 s)
  Application launch
    × click a button (6564 ms)

  ● Application launch › click a button

    Failed to create session.
    unknown error: Chrome failed to start: was killed.
      (unknown error: DevToolsActivePort file doesn't exist)
      (The process started from chrome location C:\Users\vipul\appname\src\node_modules\spectron\lib\launcher.bat is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

      at startWebDriverSession (src/node_modules/webdriver/build/utils.js:72:15)
      at Function.newSession (src/node_modules/webdriver/build/index.js:58:45)
      at Object.remote (src/node_modules/webdriverio/build/index.js:73:22)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        11.878 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I've tried to add even the args suggested by several answers in other stackoverflow questions like --headless or --no-sandbox but even those not worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-02-14 10:31:52

尝试此文件

    from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--no-sandbox");
options.add_argument("--headless")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222")  # this
options.add_argument("--disable-extensions")

#options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("enable-automation")
options.add_argument("--disable-infos")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=options)

driver.get("https://www.google.com/")
element_text = driver.page_source

print(element_text)

Try this file

    from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--no-sandbox");
options.add_argument("--headless")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--remote-debugging-port=9222")  # this
options.add_argument("--disable-extensions")

#options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("enable-automation")
options.add_argument("--disable-infos")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=options)

driver.get("https://www.google.com/")
element_text = driver.page_source

print(element_text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文