Docker ubuntu 安装 chrome chromedriver
原创
©著作权归作者所有:来自51CTO博客作者fox64194167的原创作品,请联系作者获取转载授权,否则将追究法律责任
RUN apt-get -y install xvfb gtk2-engines-pixbuf
RUN apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
RUN apt-get -y install imagemagick x11-apps
RUN Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update
RUN apt-get -y install google-chrome-stable
RUN wget http://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
RUN apt-get -y install zip
RUN apt-get -y install unzip
RUN unzip chromedriver_linux64.zip
RUN cp chromedriver /usr/bin/
要注意chromedriver的版本号,我这边最新的稳定版chrome是66,所以要用2.38的chromedriver
下面是测试代码,seleuium,上面的安装的代码我没加放requirements.txt里了。
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
time.sleep(4)
print(driver.find_element_by_id("lg"))
driver.close()
这边因为是root用户,要加--no-sandbox,不然会报错
docker container