Selenium on a Docker container

Viewed 162

I have a simple javascript file that uses selenium to launch Chrome, open some website, and collect some data from that website. Now, I want to put that file and run it inside a Docker container.

To do this I wrote the following Dockerfile:

FROM alpine
RUN apk add --update nodejs npm chromium
COPY . /src
WORKDIR /src
RUN npm install -g chromedriver
RUN npm install
ENTRYPOINT ["node", "index.js"]

The image builds with no errors, but when I attempt to run the container from it I get:

/src/node_modules/selenium-webdriver/remote/index.js:248
                reject(Error(e.message))
                       ^

Error: Server terminated early with status 1
    at /src/node_modules/selenium-webdriver/remote/index.js:248:24
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

There's only one dependency for my index.js file - "selenium-webdriver": "^4.1.1", and the file itself looks like this:

index.js

const {Builder, By} = require('selenium-webdriver');
(async function example() {
    let driver = await new Builder().forBrowser('chrome').build();
    try {
        // Navigate to Url
        await driver.get('https://www.example.com');

        // Get all the elements available with tag 'p'
        let elements = await driver.findElements(By.css('p'));
        for(let e of elements) {
            console.log(await e.getText());
        }
    }
    finally {
        await driver.quit();
    }
})();

What am I doing wrong? How can I make the container run successfully?

1 Answers

same issue, I think is with selenium-webdriver/chrome since the builder is getting stuck on mine

Related