I’ve been trying to deploy a NodeJS scraper selenium browser to docker, and host it on a dedicated server. When I run it locally, it works as intended. One of the problems I’ve encountered is that selenium can’t load an extension into the browser unless run headfully, so I’m using Xvfb – again, this works locally. I’m trying to add cookies to my driver to automate login after navigating to my desired url, which leads to an invalid cookie domain error:
Error in browser session: InvalidCookieDomainError: invalid cookie domain
(Session info: chrome=124.0.6367.155)
at Object.throwDecodedError (/app/node_modules/selenium-webdriver/lib/error.js:521:15)
at parseHttpResponse (/app/node_modules/selenium-webdriver/lib/http.js:514:13)
at Executor.execute (/app/node_modules/selenium-webdriver/lib/http.js:446:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Driver.execute (/app/node_modules/selenium-webdriver/lib/webdriver.js:740:17)
-> I fall back to logging in with a username and password if not, which again doesn’t work, and I time out with error:
error TimeoutError: Waiting for element to be located By(css selector, *[name="username"])
Wait timed out after 20081ms
at /app/node_modules/selenium-webdriver/lib/webdriver.js:911:22
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
remoteStacktrace: ''
This error only occurs when trying to run the image on the external server. The following is my Dockerfile:
# BUILD COMMAND: docker buildx build --load --platform linux/amd64 -t <name here> .
FROM node:22.1-bullseye
WORKDIR /app
COPY package.json /app
RUN npm install
RUN apt-get update && apt-get install -y wget unzip
RUN wget -q https://storage.googleapis.com/chrome-for-testing-public/124.0.6367.91/linux64/chromedriver-linux64.zip
RUN unzip chromedriver-linux64.zip
RUN mv chromedriver-linux64/chromedriver /usr/bin/chromedriver
RUN apt install libnss3-dev libgdk-pixbuf2.0-dev libgtk-3-dev libxss-dev libasound2 -y
RUN apt-get install -y xvfb
RUN wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install -y ./google-chrome-stable_current_amd64.deb
COPY index.js /app
COPY chromeExt /app/chromeExt
CMD ["node", "index.js"]
And my driver options:
options.windowSize({ width: 1400, height: 800 });
options.addArguments('--lang=en-US');
// Load chrome ext from ./chromeExt
options.addArguments('--load-extension=' + path.resolve(__dirname, './chromeExt'));
options.addArguments('--remote-debugging-pipe');
options.addArguments('--disable-gpu');
options.addArguments('--no-sandbox');
Has anyone encountered this issue, and can offer guidance? Thanks in advance!
I’ve tried running it headless, which doesn’t support loading chrome extensions. I’m sure I’m just missing one piece of this. Thanks again!
Gabriel Schull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.