|
WindowsでPythonのSeleniumラッパーSeleneを利用する。
WindowsでPythonのSeleniumラッパーSeleneを利用する方法を記載します。
Pythonをダウンロードする。
3.13だとPoetryのインストールがエラーとなってしまったので、3.12をダウンロードします。
Python公式サイト
https://www.python.org/downloads/windows/
今回はZIP版をダウンロードして進めます。
python-3.12.9-embed-amd64.zip
ダウンロード後、以下のように展開します。
C:\app\python\python-3.12.9-embed-amd64
└python.exe
環境変数を設定する。
システム環境変数を開き以下の設定を追加します。
変数名 | 変数値 |
---|---|
MY_PYTHON_HOME | C:\app\python |
MY_PYTHON_BIN | %MY_PYTHON_HOME%\python-3.12.9-embed-amd64 |
PYENV | %MY_PYTHON_HOME%.pyenv\pyenv-win |
PYENV_ROOT | %MY_PYTHON_HOME%.pyenv\pyenv-win |
PYENV_HOME | %MY_PYTHON_HOME%.pyenv\pyenv-win |
POETRY_HOME | %MY_PYTHON_HOME%.poetry\Scripts |
PATH※追記 | %MY_PYTHON_HOME%.pyenv\pyenv-win\bin %MY_PYTHON_HOME%.poetry\Scriptsenv\Scripts %MY_PYTHON_BIN% |
コマンドプロンプトを起動し以下のコマンドを実行します。
python --version
出力結果
Python 3.12.9
バージョン番号が出力されない場合、システム環境変数の設定が失敗しています。
Poetryをインストールする。
Pythonのパッケージ管理ツールPoetryをインストールします。
コマンドプロンプトを起動し以下のコマンドを実行します。
curl -sSL https://install.python-poetry.org | python -
出力結果
Retrieving Poetry metadata
# Welcome to Poetry!
This will download and install the latest version of Poetry,
a dependency and package manager for Python.
It will add the `poetry` command to Poetry's bin directory, located at:
C:\app\python\.poetry\Scripts\bin
You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.
Installing Poetry (2.1.2)
Installing Poetry (2.1.2): Creating environment
Installing Poetry (2.1.2): Installing Poetry
Installing Poetry (2.1.2): Creating script
Installing Poetry (2.1.2): Done
Poetry (2.1.2) is installed now. Great!
To get started you need Poetry's bin directory (C:\app\python\.poetry\Scripts\bin) in your `PATH`
environment variable.
You can choose and execute one of the following commands in PowerShell:
A. Append the bin directory to your user environment variable `PATH`:
'''
[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "User") + ";C:\app\python\.poetry\Scripts\bin", "User")
'''
B. Try to append the bin directory to PATH every when you run PowerShell (>=6 recommended):
'''
echo 'if (-not (Get-Command poetry -ErrorAction Ignore)) { :Path += ";C:\app\python\.poetry\Scripts\bin" }' | Out-File -Append
'''
Alternatively, you can call Poetry explicitly with `C:\app\python\.poetry\Scripts\bin\poetry`.
You can test that everything is set up by executing:
'poetry --version'
インストールできたか確認します。
poetry --version
出力結果
Poetry (version 2.1.2)
バージョン番号が出力されない場合、システム環境変数の設定が失敗しています。
最初に展開したPythonを削除する。
Poetryインストール後、自動的に以下フォルダにPythonがコピーされています。
C:\app\python.poetry\Scriptsenv\Scripts
Poetry配下のPythonを利用しないと正常に動作しないため、最初に展開したPythonを削除します。
C:\app\python\python-3.12.9-embed-amd64
pyenvをインストールする。
pyenvはPythonのバージョン管理ツールです。
Seleneが3.12だとうまく動作しないため、別バージョンをインストールするため利用します。
コマンドプロンプトを起動し以下のコマンドを実行します。
pip install pyenv-win --target %MY_PYTHON_HOME%\.pyenv
出力結果
Collecting pyenv-win
Using cached pyenv_win-3.1.1-py3-none-any.whl.metadata (24 kB)
Using cached pyenv_win-3.1.1-py3-none-any.whl (3.6 MB)
Installing collected packages: pyenv-win
Successfully installed pyenv-win-3.1.1
インストールできたか確認します。
pyenv --version
出力結果
pyenv 3.1.1
バージョン番号が出力されない場合、システム環境変数の設定が失敗しています。
Pythonのバージョンを変更する。
Seleneが動作する3.8.10に変更します。
コマンドプロンプトを起動し以下のコマンドを実行します。
pyenv install 3.8.10
出力結果
:: [Info] :: Mirror: https://www.python.org/ftp/python
:: [Downloading] :: 3.8.10 ...
:: [Downloading] :: From https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe
:: [Downloading] :: To C:\app\python\.pyenv\pyenv-win\install_cache\python-3.8.10-amd64.exe
:: [Installing] :: 3.8.10 ...
:: [Info] :: completed! 3.8.10
インストールできたか確認します。
pyenv versions
出力結果
3.8.10
3.8.10に切り替えます。
pyenv global 3.8.10
pyenv versions
出力結果
* 3.8.10 (set by C:\app\python\.pyenv\pyenv-winersion)
Poetryのプロジェクトを作成する。
Poetryのプロジェクトを作成します。
コマンドプロンプトを起動し以下のコマンドを実行します。
cd C:\app\python
poetry new selene-sample --python 3.8.10
出力結果
Created package selene_sample in selene-sample
C:\app\python\selene-sampleにプロジェクトが作成されます。
Poetryで実行するPythonバージョンを固定します。
cd C:\app\python\selene-sample
poetry env use 3.8.10
出力結果
Creating virtualenv selene-sample in C:\app\python\selene-sample\.venv
Using virtualenv: C:\app\python\selene-sample\.venv
固定されたか確認します。
poetry env info
出力結果
Virtualenv
Python: 3.8.10
Implementation: CPython
Path: C:\app\python\selene-sample\.venv
Executable: C:\app\python\selene-sample\.venv\Scripts\python.exe
Valid: True
Base
Platform: win32
OS: nt
Python: 3.8.10
Path: C:\app\python\.pyenv\pyenv-win\versions\3.8.10
Executable: C:\app\python\.pyenv\pyenv-win\versions\3.8.10\python.exe
Seleneをプロジェクトに追加します。
poetry add selene --allow-prereleases
出力結果
Using version ^2.0.0rc9 for selene
Updating dependencies
Resolving dependencies... (2.1s)
Package operations: 20 installs, 0 updates, 0 removals
- Installing attrs (25.3.0)
- Installing pycparser (2.22)
- Installing cffi (1.17.1)
- Installing exceptiongroup (1.2.2)
- Installing h11 (0.14.0)
- Installing idna (3.10)
- Installing outcome (1.3.0.post0)
- Installing sniffio (1.3.1)
- Installing sortedcontainers (2.4.0)
- Installing pysocks (1.7.1)
- Installing trio (0.27.0)
- Installing wsproto (1.2.0)
- Installing certifi (2025.1.31)
- Installing trio-websocket (0.12.2)
- Installing typing-extensions (4.13.1)
- Installing urllib3 (2.2.3)
- Installing websocket-client (1.8.0)
- Installing future (1.0.0)
- Installing selenium (4.27.1)
- Installing selene (2.0.0rc9)
Writing lock file
プロジェクトの依存関係をインストールします。
poetry install
出力結果
Installing dependencies from lock file
No dependencies to install or update
Installing the current project: selene-sample (0.1.0)
サンプル用の実行ファイルを作成します。
C:\app\python\selene-sample\src\selene_sample\sample.py
に以下の内容を入力します。
from selene import browser, be, have
browser.open('https://google.com/ncr')
browser.element('[name=q]').should(be.blank)\
.type('selenium').press_enter()
browser.all('#rso>div').should(have.size_greater_than(5))\
.first.should(have.text('Selenium automates browsers'))
サンプルを実行します。
poetry run python src\selene_sample\sample.py
GoogleChromeが立ち上がり動作すれば完了です。
※事前にGoogleChromeをPCにインストールしておく必要があります。
メモ > PHP > 変数関連
メモ > 趣味 > Bazaar
メモ > Android > Galaxy S2 LTE(SC-03D)の電池アイコンを変更する
メモ > C# > .NET Framework 4のメール送信で、長い日本語ファイル名の添付をする方法
メモ > Java > 文字列関連