1、下載並進入ubuntu鏡像
docker pull ubuntu
docker run -it --name py-selenium-firefox ubuntu bash
apt-get update
2、安裝火狐瀏覽器
apt-get install firefox
3、查看瀏覽器版本
3、安裝python及其依賴
apt-get install python3.6
apt-get install python3-pip
python3 -m pip install selenium
4、將github的上的geckodriver(https://github.com/mozilla/geckodriver/releases)下載下來,放到docker上面(docker cp命令)
tar -zxvf geckodriver-v0.26.0-linux64.tar.gz
mv geckodriver /usr/local/share/
ln -s /usr/local/share/geckodriver /usr/local/bin/geckodriver
ln -s /usr/local/share/geckodriver /usr/bin/geckodriver
5、將測試文件寫好,放到docker上(test.py)
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(firefox_options=options)
driver.get('https://blog.csdn.net/u014595589/')
print(driver.title)
driver.close()
6、退出(exit)並生成鏡像
docker commit py-selenium-firefox py-selenium-firefox
7、運行測試程序
docker run -it -e LANG=C.UTF-8 py-selenium-firefox python3 /home/test.py
鏡像地址:
https://hub.docker.com/repository/docker/homealim2012/py-selenium-firefox
參考文獻: