【初心者向けエラー解決】PythonでSeleniumを動かしてamazonにログインしてみる

スポンサーリンク
スポンサーリンク

Seleniumが動かない

『退屈なことはPythonにやらせよう』でスクレイピングを学んでいた際、Seleniumだけは、

WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

というエラーが出てきて、詰まっていました。

ChromedriverなりGeckodriverがパスの通ったファイルに存在しないというのがエラーの原因なので、

import os
os.getcwd()

で、出てくるディレクトリに

Release v0.19.0 · mozilla/geckodriver
Note that with geckodriver v0.19.0 the following versions are recommended: Firefox 55.0 (and greater) Selenium 3.5 (and greater) Added Added endpoint: POST...
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 115 or newer, please consult the Chrome for Testing availability dashboard. This page provides convenient JSON ...

などで、ダウンロードしたexeファイルを置けば解決する。

Google Colaboratoryだとうまくいかないのでドツボにハマりました。

Seleniumでamazonにログインしてみる

  1. IDを入力する
  2. PASSWORDを入力する
  3. ログインボタンを押す/入力結果を送信する

といった手順が必要。

from selenium import webdriver
browser=webdriver.Firefox()
browser.get('https://www.amazon.co.jp/ap/signin?openid.return_to=https%3A%2F%2Fwww.amazon.co.jp%2Fref%3Dgw_sgn_ib%2F357-7729046-1388829&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=jpflex&openid.mode=checkid_setup&marketPlaceId=A1VC38T7YXB528&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&')

#①IDの入力
id_elem=browser.find_element_by_id('ap_email')
id_elem.send_keys('登録したメールアドレス')
#②PASSの入力
pass_elem=browser.find_element_by_id('ap_password')
pass_elem.send_keys('登録したパスワード')
#③入力内容の送信
pass_elem.submit()

JupyterNotebookでのみ動作確認済み。

コメント

タイトルとURLをコピーしました