pseudowire

調べたことを書き残す

Selenium 4.10 を使った自動ログイン

ひさびさにSeleniumを触ったらバージョンが変わってコードも変わっていたので備忘録。 自動予約をしてくれるプログラムを作成中。

環境

参考リンク

Selenium NameError: name 'By' is not defined - Qiita

【Python】スクレイピング: SeleniumとBeautiful Soupの主なメソッド一覧 | masayanblog

Ubuntu 20.10 on Raspberry Pi 4B にSeleniumの環境を構築してスクレイピング その2:ログインしてページ遷移 - Bye Bye Moore

Selenium webdriverよく使う操作メソッドまとめ - Qiita

コード

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

#Chromeドライバの設定
options = webdriver.ChromeOptions()
service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)


#画面遷移
driver.get('https://www.amazon.co.jp/')

# ログイン画面へ遷移
driver.find_element(By.ID, "nav-link-accountList").click()

# ログインIDを入力
login_id = driver.find_element(By.ID, "ap_email")
login_id.send_keys('example@example.com')

# 「次に進む」をクリック
nextb = driver.find_element(By.CLASS_NAME, "a-button-input")
nextb.click()
time.sleep(1)

# パスワードを入力
password = driver.find_element(By.NAME, "password")
password.send_keys('example')

# 「ログイン」をクリック
nextb = driver.find_element(By.ID, "signInSubmit")
nextb.click()
time.sleep(1)