Windows+docker+selenium環境構築

Python 3.10.6
selenium 4.8.2
Docker Desktop 4.17.0

# コマンドプロンプトでpythonのバージョンを確認
python --version
# コマンドプロンプトでseleniumをバージョン指定してインストール
pip install selenium==4.8.2
# バージョン確認
python -c "import selenium; print(selenium.__version__)" 

Docker Desktop を以下からダウンロード
4.17.1は Windows defender に引っかかって入れられなったので
アーカイブから 4.17.0をダウンロードした docs.docker.com

公式のGitHub を参考に以下コマンドをコマンドプロンプトにペーストして実行する

docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:4.8.2-20230325

github.com

Desktopとかにtest.pyみたいなpythonファイル を作成して以下をペースト

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')

driver = webdriver.Remote(
    command_executor='http://localhost:4444/wd/hub',
    options=options,
)

driver.get('https://qiita.com')
print(driver.current_url)

driver.quit()

▼参考 qiita.com

コマンドプロンプトで以下を実行

python C:\Users\ユーザ名\Desktop\test.py