├── README.md ├── docker-compose.yml ├── python-selenium └── Dockerfile └── script ├── images └── .gitignore └── sample.py /README.md: -------------------------------------------------------------------------------- 1 | # selenium 2 | ## 概要 3 | Seleniumの環境構築が面倒だったため、Docker上で開発環境を再現できるように設定ファイルとサンプルスクリプトをまとめました。 4 | `docker-compose up -d`するだけでSeleniumとPythonおよびHeadless Chromeの環境が出来上がります。ブラウザを用いた処理を自動化したい場合や、テスト自動化のベースとしてご利用ください。HeadlessなのでGUI環境のないサーバー上でも動作します。 5 | 6 | ## 事前準備 7 | Dockerをインストールして、dockerコマンドとdocker-composeコマンドが使用できるようにしてください。 8 | 9 | ## 使い方 10 | ### インストールと起動方法 11 | 12 | ```bash 13 | $ git clone https://github.com/sikkimtemi/selenium 14 | $ cd selenium 15 | $ docker-compose up -d 16 | ``` 17 | 18 | 正常に起動できていれば下記のようになります。 19 | 20 | ```bash 21 | $ docker-compose ps 22 | Name Command State Ports 23 | ----------------------------------------------------------------------- 24 | chrome /opt/bin/entry_point.sh Up 0.0.0.0:5900->5900/tcp 25 | python tail -f /dev/null Up 26 | selenium-hub /opt/bin/entry_point.sh Up 0.0.0.0:4444->4444/tcp 27 | ``` 28 | 29 | ### 終了方法 30 | 31 | ```bash 32 | $ docker-compose down 33 | ``` 34 | 35 | ### サンプルスクリプトの実行 36 | 37 | ```bash 38 | $ docker exec -it python /root/script/sample.py 39 | ``` 40 | 41 | 実行するとGoogleにアクセスしてスクリーンショットを取得します。 42 | script/imagesディレクトリに画像ファイルが保存されます。 43 | 44 | ### VNC接続によるデバッグ 45 | `VNC`で接続するとブラウザの動きを確認しながらデバッグすることができます。Docker環境のIPアドレスにVNC(デフォルトは5900番ポート)でアクセスした上で、サンプルスクリプトを実行してみてください。デフォルトのパスワードは"secret"です。 46 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | selenium-hub: 2 | image: selenium/hub 3 | container_name: 'selenium-hub' 4 | ports: 5 | - 4444:4444 6 | 7 | chrome: 8 | image: selenium/node-chrome-debug 9 | container_name: 'chrome' 10 | links: 11 | - selenium-hub:hub 12 | ports: 13 | - 5900:5900 14 | volumes: 15 | - /dev/shm:/dev/shm 16 | 17 | #firefox: 18 | # image: selenium/node-firefox-debug 19 | # container_name: 'firefox' 20 | # ports: 21 | # - 5910:5900 22 | # links: 23 | # - selenium-hub:hub 24 | 25 | python: 26 | build: './python-selenium' 27 | container_name: 'python' 28 | links: 29 | - selenium-hub:hub 30 | command: 'tail -f /dev/null' 31 | working_dir: '/root/script/' 32 | volumes: 33 | - ./script/:/root/script/ 34 | environment: 35 | - 'TZ=Asia/Tokyo' 36 | -------------------------------------------------------------------------------- /python-selenium/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3 2 | 3 | # vimとseleniumをインストール 4 | RUN set -x && \ 5 | apt-get update && \ 6 | apt-get install -y vim && \ 7 | pip install selenium 8 | -------------------------------------------------------------------------------- /script/images/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /script/sample.py: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/python3 2 | from selenium import webdriver 3 | #from selenium.common.exceptions import NoSuchElementException 4 | from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 5 | from time import sleep 6 | import datetime 7 | 8 | def execSearch(browser: webdriver): 9 | """ 10 | Googleで検索を実行する 11 | :param browser: webdriver 12 | """ 13 | # スクリーンショットのファイル名用に日付を取得 14 | dt = datetime.datetime.today() 15 | dtstr = dt.strftime("%Y%m%d%H%M%S") 16 | 17 | # Googleにアクセス 18 | browser.get('https://www.google.co.jp/') 19 | sleep(1) 20 | 21 | # キーワードの入力 22 | search_box = browser.find_element_by_name("q") 23 | search_box.send_keys('docker selenium') 24 | 25 | # 検索実行 26 | search_box.submit() 27 | sleep(1) 28 | 29 | # スクリーンショット 30 | browser.save_screenshot('images/' + dtstr + '.png') 31 | 32 | 33 | if __name__ == '__main__': 34 | try: 35 | #browser = webdriver.Firefox() # 普通のFilefoxを制御する場合 36 | #browser = webdriver.Chrome() # 普通のChromeを制御する場合 37 | 38 | # HEADLESSブラウザに接続 39 | browser = webdriver.Remote( 40 | command_executor='http://selenium-hub:4444/wd/hub', 41 | desired_capabilities=DesiredCapabilities.CHROME) 42 | 43 | # Googleで検索実行 44 | execSearch(browser) 45 | 46 | finally: 47 | # 終了 48 | browser.close() 49 | browser.quit() 50 | 51 | --------------------------------------------------------------------------------