実現したいこと
・全く同じコードをコンパイルしたときに、結果が異なることの原因解明
・driver.find_elementを用いたURLの読込
前提
この質問の続きになります。
”[selenium]htmlの要素の指定・url取り出し・読込”
https://teratail.com/questions/jgkz1qj34loklg
※前回はdriver.find_elementについてでしたが、
今回は全く同じコードをコンパイルしたときに結果が異なることの原因追及のため、
別件として考えています。
下記のコードをコンパイルしたところ、全く同じコードでも結果に差が発生します。
①エラーが発生(高確率で発生)
②成功(指定のURLが表示される)
内容は全く変えていないつもりですが、うまくいったりいかなかったりします。
これに対して、
・このようなことは起こりうるのか?
・対策について(目的はURLの読込)
これらについてご教示いただきたいです。
よろしくお願いします。
発生している問題・エラーメッセージ
○失敗時 Traceback (most recent call last): File "C:\Users\○○○\Desktop\○○○\○○○\xxxxxxxx.py", line 14, in <module> btn1_name=driver.find_element(By.XPATH,'//*[@id="articleCommentModule"]/div/aside/div/div/div/span/a') File "C:\Users\○○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 738, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] File "C:\Users\○○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute self.error_handler.check_response(response) File "C:\Users\○○○\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="articleCommentModule"]/div/aside/div/div/div/span/a"} (Session info: chrome=118.0.5993.117); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception Stacktrace: GetHandleVerifier [-----------------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] GetHandleVerifier [-------------------------+----------] GetHandleVerifier [-------------------------+----------] GetHandleVerifier [-------------------------+----------] GetHandleVerifier [-------------------------+----------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] (No symbol) [--------------------------] BaseThreadInitThunk [------------------------+--] RtlUserThreadStart [-----------------------+--] ※括弧内の数列はどのようなものかわかっていないため非公開にしました ○成功時 https://news.yahoo.co.jp/articles/0fdf20fc753a62a6c4bb4e2b4e9bd9f705124d8e/comments
該当のソースコード
Python
1from selenium import webdriver 2from selenium.webdriver.chrome.service import Service 3from selenium.webdriver.common.by import By 4from selenium.webdriver.support.ui import WebDriverWait 5from selenium.webdriver.support import expected_conditions as EC 6from selenium.webdriver.support.ui import Select 7import time 8 9driver=webdriver.Chrome() #Chromeを読み込む 10driver.implicitly_wait(10) #10sまで待つ 11driver.get("https://news.yahoo.co.jp/pickup/6479600") #url入力 12time.sleep(3) 13 14btn1_name=driver.find_element(By.XPATH,'//*[@id="articleCommentModule"]/div/aside/div/div/div/span/a') 15url = btn1_name.get_attribute('href') 16print(url)#現時点ではひとまずurlを読み込むことをゴールとして、printを使っています 17
試したこと
・スペルミスの確認
・スペース、改行などの再現
補足情報(FW/ツールのバージョンなど)
windows 11
IDLE Shell 3.11.5
ChromeDriver 114.0.5735.90
selenium 4.14.0
回答2件
あなたの回答
tips
プレビュー