How To Run Selenium With Chrome In Docker

前端 未结 4 1353
孤独总比滥情好
孤独总比滥情好 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:54

    I assume you need to run it in headless mode

    https://developers.google.com/web/updates/2017/04/headless-chrome

    this is how I do it in ruby

    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {args: %w[ no-sandbox headless disable-gpu window-size=1280,1000 ]})
    
    Capybara::Selenium::Driver.new(app, :browser => :chrome, http_client: client, desired_capabilities: capabilities)
    

    You can pretty much adapt your code in python

    also consider installing some font libriaries that chrome needs to run on headless

    RUN apt-get update && \
        apt-get -qq -y install  libxpm4 libxrender1 libgtk2.0-0 libnss3\ 
           libgconf-2-4  libpango1.0-0 libxss1 libxtst6 fonts-liberation\ 
           libappindicator1 xdg-utils
    
    RUN apt-get -y install \
                   xvfb gtk2-engines-pixbuf \
                   xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable \
                   imagemagick x11-apps zip
    

提交回复
热议问题