How To Run Selenium With Chrome In Docker

前端 未结 4 1354
孤独总比滥情好
孤独总比滥情好 2021-01-31 04:56

I installed google-chrome in a Docker, but when I run my Python 2 script of Selenium, it failed like this:

automation@1c17781fef0c:/topology-editor/test$ python          


        
4条回答
  •  余生分开走
    2021-01-31 05:57

    For people coming through google search, here is the easiest workaround:

    Dockerfile

    FROM python:3.7
    
    RUN apt-get update 
    RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
    
    #download and install chrome
    RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
    
    #install python dependencies
    COPY requirements.txt requirements.txt 
    RUN pip install -r ./requirements.txt 
    
    #some envs
    ENV APP_HOME /app 
    ENV PORT 5000
    
    #set workspace
    WORKDIR ${APP_HOME}
    
    #copy local files
    COPY . . 
    
    CMD exec gunicorn --bind :${PORT} --workers 1 --threads 8 main:app 
    

    You will need to install chromedriver_binary pip package which I have added in requirements.txt as:

    Flask==1.1.1
    gunicorn==20.0.4
    selenium==3.141.0
    chromedriver-binary==79.0.3945.36
    

    Then your main.py should be like:

    from selenium import webdriver
    import chromedriver_binary
    
    
    chrome_options=webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("window-size=1400,2100") 
    chrome_options.add_argument('--disable-gpu')
    
    driver=webdriver.Chrome(chrome_options=chrome_options)
    

    Now, build your Dockerfile using docker build -t . and docker run --rm -p :5000

提交回复
热议问题