webmaster-kazu.com

テクノロジー・データ分析・実践ログ・その他

AWS EC2の「Ubuntu Server」インスタンスにPython用のSeleniumとWebdriverのインストール

カテゴリ: Amazon/AWS

AWS EC2の「Ubuntu Server」インスタンスにPython用のSeleniumとWebdriverをインストールすることについて解説しています。

なお、この記事は「AWS EC2の「Ubuntu Server」インスタンスでPython3のバージョンを確認する」という記事の続きでもあります。

■PC環境

Windows10(Google Chrome)

AWS インスタンス作成済み(Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64))

「Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)」内には「Python 3.10.4」がプレインストールされている状態となっています。

■SSHクライアント「PuTTY」を用いてSSHで接続する

AWS EC2の「Ubuntu Server」インスタンスにPython用のSeleniumとWebdriverをインストールしてみます。まず、その前に「AWS マネジメントコンソール」にログインし、「EC2」サービスへ移動します。移動後、「EC2 Management Console」内の左メニューの「インスタンス」の「インスタンス」をクリックすると、作成した「インスタンス」が一覧で表示されます。

作成した「Ubuntu Server」インスタンスの「インスタンス状態」が「実行中」であることを確認します。確認後、SSHクライアント「PuTTY」を用いて、SSHで接続を行います。

Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-1011-aws x86 64)
  • Documentation: https://help.ub
  • Management: https://landscape.canonical.com
  • Support: https://ubuntu.com/advantage

System information as of Fri Jul 8 00:49:50 UTC 2022

System load: 0.0 Usage of 7: 22.6% of 7.58GB Memory usage: 234 Swap usage: 0%

Processes:

100 Users logged in: IPv4 address for etho: *...

  • Ubuntu Pro delivers the most comprehensive open source security and,

compliance features.

https://ubuntu.com/aws/pro

42 updates can be applied immediately.

22 of these updates are standard security updates.

To see these additional updates run: apt list --upgradable

SSHで接続すると、上記のようなメッセージが出力されます。

ubuntu@ip-**-**-**-**(ipアドレス):~$

出力後、上記のように出力されれば、接続は完了となります。

■Python用のSeleniumとWebdriverのインストール

完了後、今回はPython仮想環境を作成し、SeleniumとWebdriverのインストールを行っていきます。

$ mkdir tests && cd tests

インストールのために、上記のコマンドを入力し、Enterキーを押します。「mkdir」コマンドで「test」というディレクトリを作成し、「cd」コマンドでこのディレクトリ内に移動します。

$ python3 -m venv venv

移動後、上記のコマンドを入力し、Enterキーを押します。「venv」ディレクトリの仮想化を行います。

The virtual environment was not created successfully because ensurepip is not

available. On Debian/Ubuntu systems, you need to install the python3-venv

package using the following command.

apt install python3.10-venv

You may need to use sudo with that command. After installing the python3-venv

package, recreate your virtual environment.

Failing command: ['/home/ubuntu/tests/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

Enterキーを押すと、上記のメッセージが出力されました。内容を確認すると、「ensurepipが利用できないため、仮想環境が正常に作成されませんでした。 利用できないため、仮想環境の作成に失敗しました。 Debian/Ubuntuシステムでは、以下のコマンドを使用して、python3-venv パッケージをインストールする必要があります。」という内容です。

:~/tests$ cd ..
$ sudo apt install python3.10-venv

この内容を確認し、python3-venv パッケージをインストールするために、上記のコマンドを入力し、Enterキーを押します。「sudo」権限で、python3-venv パッケージをインストールします。

Reading package lists... Done

Building dependency tree... Done

Reading state information... Done

The following additional packages will be installed:

python3-pip-whl python3-setuptools-whl

The following NEW packages will be installed:

python3-pip-whl python3-setuptools-whl python3.10-venv

0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.

Need to get 2473 kB of archives.

After this operation, 2882 kB of additional disk space will be used.

Do you want to continue? [Y/n]

Enterキーをパッケージの確認が行われ、上記のメッセージが表示されますので、「Y」と入力し、Enterキーを押します。

Get:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-pip-whl all 22.0.2+dfsg-1 [1679 kB]

Get:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3-setuptools-whl all 59.6.0-1.2 [788 kB]

Get:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu jammy/universe amd64 python3.10-venv amd64 3.10.4-3 [5660 B]

Fetched 2473 kB in 1s (3023 kB/s)

Selecting previously unselected package python3-pip-whl.

(Reading database ... 115981 files and directories currently installed.)

Preparing to unpack .../python3-pip-whl_22.0.2+dfsg-1_all.deb ...

Unpacking python3-pip-whl (22.0.2+dfsg-1) ...

Selecting previously unselected package python3-setuptools-whl.

Preparing to unpack .../python3-setuptools-whl_59.6.0-1.2_all.deb ...

Unpacking python3-setuptools-whl (59.6.0-1.2) ...

Selecting previously unselected package python3.10-venv.

Preparing to unpack .../python3.10-venv_3.10.4-3_amd64.deb ...

Unpacking python3.10-venv (3.10.4-3) ...

Setting up python3-setuptools-whl (59.6.0-1.2) ...

Setting up python3-pip-whl (22.0.2+dfsg-1) ...

Setting up python3.10-venv (3.10.4-3) ...

Scanning processes...

Scanning candidates...

Scanning linux images...

Restarting services...

Service restarts being deferred:

/etc/needrestart/restart.d/dbus.service

systemctl restart networkd-dispatcher.service

systemctl restart systemd-logind.service

systemctl restart unattended-upgrades.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Enterキーを押すと、インストールが開始され、完了となります。

$ cd tests

完了後、上記のコマンドを入力し、Enterキーを押します。「cd」コマンドで「tests」ディレクトリ内に移動します。

$ python3 -m venv venv

移動後、上記のコマンドを入力し、Enterキーを押します。再び「venv」ディレクトリの仮想化を行います。

Enterキーを押すと、今度は先程のメッセージは出力されず、何も出力されませんが、仮想化は完了しています。

$ source venv/bin/activate

その後、上記のコマンドを入力し、Enterキーを押します。仮想環境の「venv」ディレクトリ内に作成されたactivate スクリプトを実行し、仮想環境に入ります。

Enterキーを押すと、「(.venv) $」と表示されます。これで仮想環境内に入ることに成功しました。

$ pip install selenium webdriver-manager

成功後、上記のコマンドを入力し、Enterキーを押します。pipを使用して仮想環境にSeleniumおよびwebdriver-managerPythonモジュールをインストールします。

Collecting selenium

Downloading selenium-4.3.0-py3-none-any.whl (981 kB)

???????????????????????????????????????? 112.6/981.4 KB 3.2 MB/s eta 0:00:0 ???????????????????????????????????????? 450.6/981.4 KB 6.5 MB/s eta 0:00:0 ???????????????????????????????????????? 849.9/981.4 KB 8.1 MB/s eta 0:00:0 ??????????????????????????????????????? 981.4/981.4 KB 8.0 MB/s eta 0:00:00

Collecting webdriver-manager

Downloading webdriver_manager-3.8.0-py2.py3-none-any.whl (25 kB)

Collecting urllib3[secure,socks]~=1.26

Downloading urllib3-1.26.10-py2.py3-none-any.whl (139 kB)

?????????????????????????????????????? 139.2/139.2 KB 13.7 MB/s eta 0:00:00

Collecting trio~=0.17

Downloading trio-0.21.0-py3-none-any.whl (358 kB)

?????????????????????????????????????? 359.0/359.0 KB 11.3 MB/s eta 0:00:00

Collecting trio-websocket~=0.9

Downloading trio_websocket-0.9.2-py3-none-any.whl (16 kB)

Collecting requests

Downloading requests-2.28.1-py3-none-any.whl (62 kB)

???????????????????????????????????????? 62.8/62.8 KB 11.3 MB/s eta 0:00:00

Collecting pybrowsers

Downloading pybrowsers-0.5.0-py3-none-any.whl (8.3 kB)

Collecting python-dotenv

Downloading python_dotenv-0.20.0-py3-none-any.whl (17 kB)

Collecting sortedcontainers

Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)

Collecting async-generator>=1.9

Downloading async_generator-1.10-py3-none-any.whl (18 kB)

Collecting outcome

Downloading outcome-1.2.0-py2.py3-none-any.whl (9.7 kB)

Collecting sniffio

Downloading sniffio-1.2.0-py3-none-any.whl (10 kB)

Collecting attrs>=19.2.0

Downloading attrs-21.4.0-py2.py3-none-any.whl (60 kB)

???????????????????????????????????????? 60.6/60.6 KB 9.9 MB/s eta 0:00:00

Collecting idna

Downloading idna-3.3-py3-none-any.whl (61 kB)

???????????????????????????????????????? 61.2/61.2 KB 12.4 MB/s eta 0:00:00

Collecting wsproto>=0.14

Downloading wsproto-1.1.0-py3-none-any.whl (24 kB)

Collecting PySocks!=1.5.7,<2.0,>=1.5.6

Downloading PySocks-1.7.1-py3-none-any.whl (16 kB)

Collecting certifi

Downloading certifi-2022.6.15-py3-none-any.whl (160 kB)

?????????????????????????????????????? 160.2/160.2 KB 11.3 MB/s eta 0:00:00

Collecting pyOpenSSL>=0.14

Downloading pyOpenSSL-22.0.0-py2.py3-none-any.whl (55 kB)

???????????????????????????????????????? 55.8/55.8 KB 10.5 MB/s eta 0:00:00

Collecting cryptography>=1.3.4

Downloading cryptography-37.0.4-cp36-abi3-manylinux_2_24_x86_64.whl (4.1 MB)

???????????????????????????????????????? 4.1/4.1 MB 18.7 MB/s eta 0:00:00

Collecting pyxdg<0.29,>=0.27

Downloading pyxdg-0.28-py2.py3-none-any.whl (49 kB)

???????????????????????????????????????? 49.5/49.5 KB 9.2 MB/s eta 0:00:00

Collecting charset-normalizer<3,>=2

Downloading charset_normalizer-2.1.0-py3-none-any.whl (39 kB)

Collecting cffi>=1.12

Downloading cffi-1.15.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (441 kB)

?????????????????????????????????????? 441.8/441.8 KB 22.5 MB/s eta 0:00:00

Collecting h11<1,>=0.9.0

Downloading h11-0.13.0-py3-none-any.whl (58 kB)

???????????????????????????????????????? 58.2/58.2 KB 10.6 MB/s eta 0:00:00

Collecting pycparser

Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)

?????????????????????????????????????? 118.7/118.7 KB 16.2 MB/s eta 0:00:00

Installing collected packages: sortedcontainers, pyxdg, urllib3, sniffio, python-dotenv, PySocks, pycparser, pybrowsers, idna, h11, charset-normalizer, certifi, attrs, async-generator, wsproto, requests, outcome, cffi, webdriver-manager, trio, cryptography, trio-websocket, pyOpenSSL, selenium

Successfully installed PySocks-1.7.1 async-generator-1.10 attrs-21.4.0 certifi-2022.6.15 cffi-1.15.1 charset-normalizer-2.1.0 cryptography-37.0.4 h11-0.13.0 idna-3.3 outcome-1.2.0 pyOpenSSL-22.0.0 pybrowsers-0.5.0 pycparser-2.21 python-dotenv-0.20.0 pyxdg-0.28 requests-2.28.1 selenium-4.3.0 sniffio-1.2.0 sortedcontainers-2.4.0 trio-0.21.0 trio-websocket-0.9.2 urllib3-1.26.10 webdriver-manager-3.8.0 wsproto-1.1.0

Enterキーを押すと、インストールが開始されます。しばらくすると、「Successfully installed(インストールに成功しました)」と出力されます。これでインストールは完了となります。