jupyternotebookでselenium(with docker-compose)


はじめに

VNCでseleniumを試したかったんだけど、docker-machineで建てたホストはリモートのため、なかなか上手くいかなかった。とりあえず動かせたから、備忘録として残しておく。

環境

seleniumを起動する方:
docker-machineで建てたubuntu

VNCして確認する方:
macOS Mojave 10.14.16

なので、Finder→移動→サーバーへ接続を利用する。
接続先は、http://ホストのIP:5900で、パスワードはsecret

docker-compose

自分の環境の場合は以下の通りにしてくれれば動く。いじる場合は自己責任でやってください。Dockerfileの中身はjupyter-notebook/scpiyの公式のやつに、少し手を加えただけのものです。

$ mkdir Jupyter 
$ cd Jupyter
$ touch Dockerfile
$ touch docker-compose.yml
$ mkdir work
Dockerfile
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
ARG BASE_CONTAINER=jupyter/minimal-notebook
FROM $BASE_CONTAINER

LABEL maintainer="Jupyter Project <[email protected]>"

USER root

# ffmpeg for matplotlib anim & dvipng for latex labels
RUN apt-get update && \
    apt-get install -y --no-install-recommends ffmpeg dvipng && \
    rm -rf /var/lib/apt/lists/*

USER $NB_UID

# Install Python 3 packages
RUN conda install --quiet --yes \
    'beautifulsoup4=4.8.*' \
    'conda-forge::blas=*=openblas' \
    'bokeh=1.4.*' \
    'cloudpickle=1.3.*' \
    'cython=0.29.*' \
    'dask=2.11.*' \
    'dill=0.3.*' \
    'h5py=2.10.*' \
    'hdf5=1.10.*' \
    'ipywidgets=7.5.*' \
    'ipympl=0.5.*'\
    'matplotlib-base=3.1.*' \
    'numba=0.48.*' \
    'numexpr=2.7.*' \
    'pandas=1.0.*' \
    'patsy=0.5.*' \
    'protobuf=3.11.*' \
    'scikit-image=0.16.*' \
    'scikit-learn=0.22.*' \
    'scipy=1.4.*' \
    'seaborn=0.10.*' \
    'sqlalchemy=1.3.*' \
    'statsmodels=0.11.*' \
    'sympy=1.5.*' \
    'vincent=0.4.*' \
    'widgetsnbextension=3.5.*'\
    'xlrd' \
    && \
    conda clean --all -f -y && \
    # Activate ipywidgets extension in the environment that runs the notebook server
    jupyter nbextension enable --py widgetsnbextension --sys-prefix && \
    # Also activate ipywidgets extension for JupyterLab
    # Check this URL for most recent compatibilities
    # https://github.com/jupyter-widgets/ipywidgets/tree/master/packages/jupyterlab-manager
    jupyter labextension install @jupyter-widgets/jupyterlab-manager@^2.0.0 --no-build && \
    jupyter labextension install @bokeh/jupyter_bokeh@^2.0.0 --no-build && \
    jupyter labextension install jupyter-matplotlib@^0.7.2 --no-build && \
    jupyter lab build && \
    jupyter lab clean && \
    npm cache clean --force && \
    rm -rf /home/$NB_USER/.cache/yarn && \
    rm -rf /home/$NB_USER/.node-gyp && \
    fix-permissions $CONDA_DIR && \
    fix-permissions /home/$NB_USER

# Install facets which does not have a pip or conda package at the moment
RUN cd /tmp && \
    git clone https://github.com/PAIR-code/facets.git && \
    cd facets && \
    jupyter nbextension install facets-dist/ --sys-prefix && \
    cd && \
    rm -rf /tmp/facets && \
    fix-permissions $CONDA_DIR && \
    fix-permissions /home/$NB_USER

# Import matplotlib the first time to build the font cache.
ENV XDG_CACHE_HOME /home/$NB_USER/.cache/
RUN MPLBACKEND=Agg python -c "import matplotlib.pyplot" && \
    fix-permissions /home/$NB_USER

USER $NB_UID

WORKDIR /home/jovyan/work
docker-compose.yml
version: "3"
services:
  selenium-standalone-chrome-debug:
    image: selenium/standalone-chrome-debug
    container_name: selenium-hub
    ports:
      - "4444:4444"
      - "5900:5900"
    volumes:
      - /dev/shm:/dev/shm

  jupyter-notebook:
    build: .
    container_name: jupyter-notebook
    ports:
      - "8888:8888"
    volumes:
      - ./work:/home/jovyan/work
    tty: true

これでコードの準備は完了です。あとはコンテナを建てていきます。

$ docker-compose build
$ docker-compose up -d

# コンテナが建っているか確認
$ docker-compose ps -a

コンテナへの入り方を示しておきます。

$ docker-compose exec jupyter-notebook bash

# rootで入りたい時
$ docker-compose exec --user=root jupyter-notebook bash

コンテナに入れたら、通常のjupyter notebookと同じように利用することがきます。また、workディレクトリ以下は共有ファイルとなっています。

seleniumを試す

ホストのIPをあらかじめ準備しておいてください。
コンテナが動いているかをdocker-compose psで確認できたら、まず以下のURLにアクセスできるか試してみてください。

http://ホストのIP:4444/wd/hub/

このページが表示できれば順調です。

次に、MacならFinderから移動でサーバーへ接続とすることができるはずなのでそれを利用します。VNCできるクライアントがあればおkです。
接続するURLは、vnc://ホストのIP:5900でパスワードはsecretです。

ここまで上手くいっていれば、あと1手順で接続できます。

Jupyter notebookから動かす

公式のページの一番下にありました。
リモートWebDriverでSeleniumを使用する

jupyter notebookであれば、1行ごとに実行できると思うので試していってみてください。

selenium.py
! pip install selenium
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
   command_executor='http://ホストのIP:4444/wd/hub',
   desired_capabilities=DesiredCapabilities.CHROME)

多少時間はかかりますが、待っていると表示されると思います。
seleniumの使い方は調べてください。

参考

Docker上でSeleniumとHeadless ChromeとPython3を動かす