ここのサイト
にも同様の質問がされていますが、
公式のサイトのWebDriverJsを参考にして、Electron上でSeleniumを実行したいのですが、以下のエラーが出てElectronが起動しません。
(node:16636) UnhandledPromiseRejectionWarning: SessionNotCreatedError: session not created: No matching capabilities found at Object.throwDecodedError (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/error.js:550:15) at parseHttpResponse (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/http.js:565:13) at Executor.execute (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/http.js:491:26) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:16636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:16636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:16636) UnhandledPromiseRejectionWarning: SessionNotCreatedError: session not created: No matching capabilities found at Object.throwDecodedError (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/error.js:550:15) at parseHttpResponse (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/http.js:565:13) at Executor.execute (/Users/xxx/Desktop/Electron/quick/electron-quick-start/node_modules/selenium-webdriver/lib/http.js:491:26) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:16636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Version
chromium: 85.0.4183.93
Electron: 10.1.1.
ChromeDriver 85.0.4183.84
ソースコードはhttps://github.com/electron/electron-quick-startのmain.jsにseleniumのコードを加えただけです。
導入はhttps://www.electronjs.org/の公式にあるように、
# Quick Start リポジトリを clone する $ git clone https://github.com/electron/electron-quick-start # リポジトリに移動 $ cd electron-quick-start # 依存ライブラリをインストールし、実行 $ npm install $ npm start
に従いました。
なお、seleniumを加えた追加箇所のコード(main.js)は
main.JS
1// Modules to control application life and create native browser window 2const { app, BrowserWindow } = require("electron"); 3const path = require("path"); 4 5 6const webdriver = require("selenium-webdriver"); 7 8function createWindow() { 9 // Create the browser window. 10 const mainWindow = new BrowserWindow({ 11 width: 800, 12 height: 600, 13 webPreferences: { 14 preload: path.join(__dirname, "preload.js"), 15 }, 16 }); 17 18 // and load the index.html of the app. 19 mainWindow.loadFile("index.html"); 20 21 // Open the DevTools. 22 // mainWindow.webContents.openDevTools() 23 24///////////////////////////加えたselenium部分////////////////////////////////// 25 const driver = new webdriver.Builder() 26 // The "9515" is the port opened by chrome driver. 27 .usingServer("http://localhost:9515") 28 .withCapabilities({ 29 chromeOptions: { 30 // Here is the path to your Electron binary. 31 binary: 32 "/Users/s_o813/Desktop/Electron/quick/electron-quick-start/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron", 33 }, 34 }) 35 .forBrowser("electron") 36 .build(); 37 38 driver.get("http://www.google.com"); 39 driver.findElement(webdriver.By.name("q")).sendKeys("webdriver"); 40 driver.findElement(webdriver.By.name("btnG")).click(); 41 driver.wait(() => { 42 return driver.getTitle().then((title) => { 43 return title === "webdriver - Google Search"; 44 }); 45 }, 1000); 46} 47///////////////////////////////////////////////////////////////////////// 48 49// This method will be called when Electron has finished 50// initialization and is ready to create browser windows. 51// Some APIs can only be used after this event occurs. 52app.whenReady().then(() => { 53 createWindow(); 54 55 app.on("activate", function () { 56 // On macOS it's common to re-create a window in the app when the 57 // dock icon is clicked and there are no other windows open. 58 if (BrowserWindow.getAllWindows().length === 0) createWindow(); 59 }); 60}); 61 62// Quit when all windows are closed, except on macOS. There, it's common 63// for applications and their menu bar to stay active until the user quits 64// explicitly with Cmd + Q. 65app.on("window-all-closed", function () { 66 if (process.platform !== "darwin") app.quit(); 67}); 68 69// In this file you can include the rest of your app's specific main process 70// code. You can also put them in separate files and require them here. 71
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。