Electron selenium webdriver ipc script not working in build

I have an electron project that renders an html page with a button that when clicked calls a node js script (via IPC) which uses selenium to scrape webpages.

here’s my project structure:

-app/
 --index.html
 --main.js
 --index.js
-package.json

Here’s my package.json:

{
    "name": "agencies-scraper",
    "version": "1.0.0",
    "main": "app/main.js",
    "devDependencies": {
        "electron": "^5.0.7",
        "electron-builder": "^21.1.1"
    },
    "scripts": {
        "start": "electron .",
        "pack": "electron-builder --dir",
        "dist": "electron-builder",
        "postinstall": "electron-builder install-app-deps"
    },
    "build": {
        "appId": "my.id",
        "files": [
            "app/**/*",
            "node_modules/**/*",
            "package.json"
        ],
        "mac": {
            "category": "your.app.category.type"
        },
    },
    "dependencies": {
        "csv-writer": "^1.5.0",
        "selenium-webdriver": "^4.0.0-alpha.4"
    }
}

In index.html I have a button:

    <button id="test">Click to Scrape</button>

When clicked it fires the following chain of reactions:

  1. The linked index.js, send a signal to the “backchannel” ipc channel :
const {ipcRenderer} = require("electron");
const button = document.getElementById("test");
button.addEventListener("click", () => {
  ipcRenderer.send("backchannel"); 
}
  1. In main.js, I listen to “backchannel”, and when triggered, instantiate a selenium webdriver and open google.com in a separate chrome browser:
const {app, BrowserWindow, ipcMain} = require('electron')
const webdriver = require('selenium-webdriver')

ipcMain.on('backchannel', async (event, arg) => {
    const driver = new webdriver.Builder()
            .forBrowser('chrome')
            .build()
    try {
        await driver.get('https://google.com');
    } catch (error) {
        console.log(error)
    }
})
  1. The os asks if I want the electron app to accept incoming connections and I click “Allow” (selenium opens a browser regardless):
    31%20PM
    And a chrome browser opens & navigates to google.com

This works perfectly when I work in development via npm start.

However, when I package the app either via npm run-script pack or npm run-script dist, the build version does not reach step 3. No permission window pops up and no browser opens. Selenium just doesn’t work there.

I am sure the IPC is working from index.js to main.js in the build version so the issue is not with that. What am I missing here?

I had the same problem. Can you share your github?

I have fixed it.

// package.json
{
...
  "dependencies": {
    "chromedriver": "^76.0.0",
    "selenium-webdriver": "^4.0.0-alpha.4"
  }
...
}
#main.js
const webdriver = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');
const chromePath = require('chromedriver').path;

const Options = chrome.Options;
const service = new chrome.ServiceBuilder(chromePath).build();
chrome.setDefaultService(service);

ipcMain.on('backchannel', async (event, arg) => {
    const driver = new webdriver.Builder()
        .forBrowser('chrome')
        .withCapabilities(webdriver.Capabilities.chrome())
        .setChromeOptions(new Options().excludeSwitches(["ignore-certificate-errors", chromePath]))
        .build()
    try {
        await driver.get('http://www.google.com');
        await driver.sleep(3000);
    } catch (error) {
        event.reply('backchannel-reply', error);
        console.log(error)
    } finally {
        await driver.quit();
    }
})