Score:0

How to pass the chromedriver path to nodejs selenium-webdriver in Docker

us flag
Jay

Here is my docker file.

FROM node:17.2.0

USER root

WORKDIR /LT

RUN apt-get update
  
RUN apt-get install git --yes

COPY /LT .
COPY /LT/test .
COPY ["/LT/package.json", "."]

# Install Google Chrome
RUN apt-get install wget
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN apt-get install ./google-chrome*.deb --yes

ENV CHROME_BIN=/usr/bin/google-chrome

RUN npm install
RUN npm ci
RUN npm install nodejs
RUN npm install mocha -g
RUN npm install chromedriver -g --unsafe-perm
RUN npm install selenium-webdriver
RUN npm install webdriver-manager
#RUN webdriver-manager update
CMD ["node", "./test/script1.js"]

Following is my nodejs javascript file which uses chrome to launch the application and login.

require("chromedriver");
const {By,Key,Builder} = require("selenium-webdriver");
const chrome = require('selenium-webdriver/chrome');

 
async function getAuthCode(){ 

        const url = "https://abcd.com";
        
       //To wait for browser to build and launch properly
        let driver = await new Builder()
            .forBrowser("chrome")
            .setChromeOptions(new chrome.Options().headless()) 
            
            //.setChromeOptions(new chrome.Options().addArguments(['--no-sandbox','-headless', '--disable-dev-shm-usage']))           
        
            .build();
        
        await driver.get(url);
        console.log(driver);
        const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
        await wait(1 * 20 * 1000);
        await driver.manage().window().setRect({ width: 1552, height: 840 });
        await driver.findElement(By.id("emailInput")).click();
        await driver.findElement(By.id("emailInput")).sendKeys("[email protected]");
        
        await driver.findElement(By.id("submitbutton")).click();
        
        
        
        await wait(1 * 20 * 1000);
       
        
        await driver.quit();
        
        return 'some data';

}
 
async function testAuthCode()
{
    var cCode = await getAuthCode();
    console.log(cCode);
}
testAuthCode();

When I run my docker compose file using the same docker file which is given above, I get following error.

WebDriverError: unknown error: Chrome failed to start: exited abnormally.
-nodejs-1  |   (unknown error: DevToolsActivePort file doesn't exist)
-nodejs-1  |   (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
-nodejs-1  |     at Object.throwDecodedError (/LT/node_modules/selenium-webdriver/lib/error.js:539:15)

I know the reason , It can not find the chromedriver at the default path '/usr/bin/google-chrome'.

Either

  1. I have to install chrome at the default location given Or
  2. give the path of that location where chrome is actually installed.

In either case, How do I do it in JavaScript or docker?

PS: The script runs perfectly fine in my local windows environment.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.