Chromeドライバーのインストール
- Chromeのバージョンを確認
- 利用しているChromeに対応するChromeドライバーを選択
- OSに対応しているドライバーをインストール
https://googlechromelabs.github.io/chrome-for-testing
Seleniumのインストール
pip install selenium
from selenium import webdriver
target = 'https://example.com'
# path = '/Users/xxx/Downloads/chromedriver'
driver = webdriver.Chrome() # pathを書くとエラーが発生する
driver.get(target) // ウィンドウを開く
driver.quit() // ウィンドウを閉じる
ドライバーを指定する場合
https://googlechromelabs.github.io/chrome-for-testing
Binaryがchromedriverの欄から該当するPlatformのURLからドライバーをダウンロード
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
target = 'https://example.com'
path = '/Users/xxx/Downloads/chromedriver-mac-arm64/chromedriver'
service = Service(executable_path=path)
driver = webdriver.Chrome(service=service)
driver.get(target)
driver.quit()
エラーが出た場合、認証するか手動で一度driverを開く
driverを開くとターミナルで以下の応答が表示され、Seleniumから開けるようになる
/Users/xxx/Downloads/chromedriver-mac-arm64/chromedriver ; exit;
(base) xxx ~ % /Users/xxx/Downloads/chromedriver-mac-arm64/chromedriver ; exit;
Starting ChromeDriver 128.0.6613.84 (xxx-refs/branch-heads/9999@{#9999}) on port 0
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully on port 52983.
終了方法? ターミナルを閉じる
このウインドウで実行中のプロセスを強制的に終了しますか?→終了
要素の探し方(3.x)
driver.find_elemnent_by_id('id')
driver.find_elemnent_by_class_name('class_name')
driver.find_elemnent_by_tag_name('tag')
driver.find_elemnent_by_xpath('//tag[@AttributeName="Value"]')
driver.find_elemnent_by_css_selector()
driver.find_elemnent_by_name()
driver.find_elemnent_by_link_text()
driver.find_elemnents_by_class_name() // [a, b, c]
要素の探し方(4.x)
driver.find_element(by="id", value="xxx")
driver.find_elements(by="xpath", value="xxx")
Seleniumのインストール時の表示
Collecting selenium
Downloading selenium-4.23.1-py3-none-any.whl.metadata (7.1 kB)
Requirement already satisfied: urllib3<3,>=1.26 in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from urllib3[socks]<3,>=1.26->selenium) (2.2.1)
Collecting trio~=0.17 (from selenium)
Downloading trio-0.26.2-py3-none-any.whl.metadata (8.6 kB)
Collecting trio-websocket~=0.9 (from selenium)
Downloading trio_websocket-0.11.1-py3-none-any.whl.metadata (4.7 kB)
Requirement already satisfied: certifi>=2021.10.8 in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from selenium) (2024.2.2)
Requirement already satisfied: typing_extensions~=4.9 in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from selenium) (4.10.0)
Collecting websocket-client~=1.8 (from selenium)
Downloading websocket_client-1.8.0-py3-none-any.whl.metadata (8.0 kB)
Requirement already satisfied: attrs>=23.2.0 in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from trio~=0.17->selenium) (23.2.0)
Collecting sortedcontainers (from trio~=0.17->selenium)
Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl.metadata (10 kB)
Requirement already satisfied: idna in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from trio~=0.17->selenium) (3.6)
Collecting outcome (from trio~=0.17->selenium)
Downloading outcome-1.3.0.post0-py2.py3-none-any.whl.metadata (2.6 kB)
Collecting sniffio>=1.3.0 (from trio~=0.17->selenium)
Downloading sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)
Collecting wsproto>=0.14 (from trio-websocket~=0.9->selenium)
Downloading wsproto-1.2.0-py3-none-any.whl.metadata (5.6 kB)
Requirement already satisfied: pysocks!=1.5.7,<2.0,>=1.5.6 in /opt/anaconda3/envs/my_scrapy_venv/lib/python3.11/site-packages (from urllib3[socks]<3,>=1.26->selenium) (1.7.1)
Collecting h11<1,>=0.9.0 (from wsproto>=0.14->trio-websocket~=0.9->selenium)
Downloading h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)
Downloading selenium-4.23.1-py3-none-any.whl (9.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.4/9.4 MB 30.9 MB/s eta 0:00:00
Downloading trio-0.26.2-py3-none-any.whl (475 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 476.0/476.0 kB 32.5 MB/s eta 0:00:00
Downloading trio_websocket-0.11.1-py3-none-any.whl (17 kB)
Downloading websocket_client-1.8.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.8/58.8 kB 6.8 MB/s eta 0:00:00
Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)
Downloading wsproto-1.2.0-py3-none-any.whl (24 kB)
Downloading outcome-1.3.0.post0-py2.py3-none-any.whl (10 kB)
Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 6.1 MB/s eta 0:00:00
Installing collected packages: sortedcontainers, websocket-client, sniffio, outcome, h11, wsproto, trio, trio-websocket, selenium
Successfully installed h11-0.14.0 outcome-1.3.0.post0 selenium-4.23.1 sniffio-1.3.1 sortedcontainers-2.4.0 trio-0.26.2 trio-websocket-0.11.1 websocket-client-1.8.0 wsproto-1.2.0