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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

2792閲覧

python、selenium4でfind_elementを機能させる

sigefuji

総合スコア125

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/03/25 06:16

編集2023/03/25 06:39

実現したいこと

find_elementを動作させる
### 前提

chromeのselenium4でwebページを制御しようとしていますが、入り口の文字を入力するコントロールにたいして、
find_elementを動作させることができません。

発生している問題・エラーメッセージ

このエラーの意味がわかりません。 
"using": by, "value": value})["value"]

c:\python>python test.py c:\python\test.py:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome("c:/driver/chromedriver.exe") DevTools listening on ws://127.0.0.1:64234/devtools/browser/437d7bdd-b2d2-45b7-8dda-de9852ff051c [5204:2824:0325/145106.652:ERROR:page_load_metrics_update_dispatcher.cc(178)] Invalid first_paint (unset) for first_image_paint 1.955 s [5204:2824:0325/145106.652:ERROR:page_load_metrics_update_dispatcher.cc(178)] Invalid first_paint 2.037 s for first_image_paint 1.955 s Traceback (most recent call last): File "c:\python\test.py", line 5, in <module> email = driver.find_element(By.ID,"user-sign-in-email") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\qhtsi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 830, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\qhtsi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute [5204:2824:0325/145106.810:ERROR:page_load_metrics_update_dispatcher.cc(178)] Invalid first_paint 2.037 s for first_image_paint 1.955 s self.error_handler.check_response(response) File "C:\Users\qhtsi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="user-sign-in-email"]"} (Session info: chrome=111.0.5563.111) Stacktrace: Backtrace:

該当のソースコード

python

1from selenium import webdriver 2from selenium.webdriver.common.by import By 3driver = webdriver.Chrome("c:/driver/chromedriver.exe") 4driver.get("https://eas.forexsb.com") 5email = driver.find_element(By.ID,"user-sign-in-email") 6email.send_keys("****@outlook.jp") 7elem_login_pw = driver.find_element(By.ID,"user-sign-in-password").send_keys("qACSqwvD") 8elem_login_email = driver.find_element(By.ID,"user-sign-in-submit").send_keys("keys.ENTER") 9

試したこと

ChromeのF12で、当該コントロール(文字列入力)を指定して、強調表示されたところ見る限り、IDの文字列は存在しているように見えます。この部分のカット&ペーストを試みましたが失敗しました。

補足情報(FW/ツールのバージョンなど)

python 3.11.1
selenium 4.8.2

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

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

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

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

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

sigefuji

2023/03/25 06:40 編集

質問を編集しました
meg_

2023/03/25 06:57

ページの読み込みが完了する前にfind_elementを実行している可能性はありませんか?
EJ000124

2024/02/22 05:06 編集

どういった画面を出したいかにもよりますが、 Edgeでの環境でしたら、いけました。。。。 from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://eas.forexsb.com") email = driver.find_element(By.ID,"user-sign-in-email") email.send_keys("xxxx@〜") elem_login_pw = driver.find_element(By.ID,"user-sign-in-password").send_keys("xxxxxxx") elem_login_email = driver.find_element(By.ID,"user-sign-in-submit").send_keys("keys.ENTER")
EJ000124

2024/02/22 05:03

ちなみに、コードを確認してみると、GoogleChromeを利用されてたようですが、WEBドライバーとブラウザのバージョンは同じになってますでしょうか? Edgeでいけたので、もしかしたらWEBドライバーとブラウザのバージョンかな?とは思いましたが如何でしょう?
guest

回答1

0

自己解決

meg_さんのアドバイスで記事
https://codechacha.com/ja/selenium-explicit-implicit-wait/
を参考にして
wait処理を追加して、この問題は解決しました。

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

driver = webdriver.Chrome("c:/driver/chromedriver.exe")
#driver.implicitly_wait(10) # seconds
driver.get("https://eas.forexsb.com")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'user-sign-in-email')))
EC.element_to_be_clickable((By.ID, 'user-sign-in-email'))
email = driver.find_element(By.ID,"user-sign-in-email")
email.send_keys("y.f-oka@outlook.jp")
elem_login_pw = driver.find_element(By.ID,"user-sign-in-password").send_keys("qACSqwvD")
elem_login_email = driver.find_element(By.ID,"user-sign-in-submit").send_keys("keys.ENTER")

input()

投稿2023/03/25 11:44

sigefuji

総合スコア125

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問