質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

4回答

2224閲覧

webdriverで自動ログインが出来ない理由

KOO_

総合スコア58

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/10/15 17:23

編集2019/10/16 07:16

はじめまして。現在pythonのseleniumを使い、サイトへの自動ログインを試みております。

chormedriverでpython file.pyをすると無事ファイルは開くのですが、ログイン・パスが自動入力されるはずが何度行なっても入力が行われません。

エラー文にはclassに対するエラーが出ておりますが、エラー解消を特定出来ておりません。

どなたかご教示頂けますでしょうか?

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys import urllib.parse import time driver = webdriver.Chrome("/Users/***/chromedriver") driver.get("https://***/accounts/login/") #ログインID・PWを入力 elem_search_word = driver.find_element_by_class_name("_2hvTZ") elem_search_word.send_keys("****") password = driver.find_element_by_name('password') password.send_keys("****") password.send_keys(Keys.ENTER) driver.implicitly_wait(5)
File "instagram.py", line 14, in <module> elem_search_word = driver.find_element_by_class_name("_2hvTZ") File "/Users/***/.pyenv/versions/3.7.4/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name return self.find_element(by=By.CLASS_NAME, value=name) File "/Users/***/.pyenv/versions/3.7.4/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element 'value': value})['value'] File "/Users/***/.pyenv/versions/3.7.4/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/***/.pyenv/versions/3.7.4/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"_2hvTZ"} (Session info: chrome=77.0.3865.120) (Driver info: chromedriver=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.14.6 x86_64)
<div class="EPjEi"> <form class="HmktE" method="post"> <div class=" Igw0E IwRSH eGOV_ _4EzTm FBi-h "> </div> <div class="-MzZI"> <div class="_9GP1n "> <label class="f0n8F "> <span class="_9nyy2">電話番号、ユーザーネーム、メールアドレス</span> <input class="_2hvTZ pexuQ zyHYP" aria-label="電話番号、ユーザーネーム、メールアドレス" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" value=""> </label> <div class="i24fI"> </div> </div> </div> <div class="-MzZI"> <div class="_9GP1n "> <label class="f0n8F "> <span class="_9nyy2">パスワード</span> <input class="_2hvTZ pexuQ zyHYP" aria-label="パスワード" aria-required="true" autocapitalize="off" autocorrect="off" name="password" type="password" value=""></label> <div class="i24fI"></div></div></div> <div class=" Igw0E IwRSH eGOV_ _4EzTm bkEs3 CovQj jKUp7 DhRcB "> <button class="sqdOP L3NKy y3zKF " disabled="" type="submit"> <div class=" Igw0E IwRSH eGOV_ _4EzTm ">ログイン</div></button></div> <div class="K-1uj Z7p_S"><div class="s311c"></div><div class="_0tv-g">または</div><div class="s311c"></div></div> <div class=" Igw0E IwRSH eGOV_ _4EzTm bkEs3 CovQj jKUp7 DhRcB "> <button class="sqdOP yWX7d y3zKF " type="button"> <span class="coreSpriteFacebookIcon AeB99"></span> <span class="KPnG0">Facebookでログイン</span></button></div> <a class="_2Lks6" href="/accounts/password/reset/">パスワードを忘れた場合</a></form></div>

以上よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

t_obara

2019/10/16 01:38

エラーが出ているのであれば、表示されている全ての情報をご提示ください。
KOO_

2019/10/16 07:16

該当htmlとエラー内容追加をしました。 よろしくお願い致します。
guest

回答4

0

デバッガを使って、もしくは対話型環境で1文ずつ実行して、それとChromeの開発者ツールを両方見比べながら、1ステップずつ進めるとできると思います。

投稿2019/10/16 12:58

otn

総合スコア84491

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOO_

2019/10/17 08:18

ありがとう御座います。
guest

0

サイトによっては、Botによる自動アクセスを拒否している場合があります。

たとえば、reCAPTCHA v3というライブラリがあって、ユーザーによる意識的な操作がなくても、アクセスしているユーザーエージェントが自動アクセスかどうかの「怪しさ」を取得できるようになっています。

投稿2019/10/16 00:17

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

Python

1from selenium import webdriver 2from selenium.webdriver.common.by import By 3from selenium.webdriver.support.ui import WebDriverWait 4from selenium.webdriver.support import expected_conditions as EC 5from selenium.webdriver.common.keys import Keys 6import urllib.parse 7import time 8 9 10driver = webdriver.Chrome("/Users/***/chromedriver") 11driver.get("https://***/accounts/login/") 12 13driver.implicitly_wait(5) 14#ログインID・PWを入力 15elem_search_word = driver.find_element_by_css_selector("#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(2) > div > label > input") 16elem_search_word.send_keys("****") 17password = driver.find_element_by_name('password') 18password.send_keys("*******") 19password.send_keys(Keys.ENTER) 20driver.implicitly_wait(5)

投稿2019/12/07 02:15

Reach

総合スコア733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

アクセスするWebブラウザによって出力する HTML を変えているサイト等もありますが、selenium でアクセスした際にその class 名は存在していますか?

driver.get で、 URL にアクセスした後に、driver.page_source 等の方法で、取得した HTML の内容を確認してみてください。
探している class 名は HTML の中に存在しているでしょうか?

投稿2019/10/15 21:41

編集2019/10/15 21:42
CHERRY

総合スコア25171

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOO_

2019/10/16 07:23

お世話になっております。 確認したところ存在はしているようです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問