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:
- 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");
}
- 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)
}
})
- The os asks if I want the electron app to accept incoming connections and I click “Allow” (selenium opens a browser regardless):
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?