困っていること
- find_elementで取得できない要素を拾いたい
初心者ながら、pythonでseleniumを使ってメルカリのスクレイピングをする検証をしていますが、一部の要素がどうしても抜けません
ページの要素
該当のソースコード
python
1# Chromeドライバの自動更新 2import chromedriver_binary_sync 3chromedriver_binary_sync.download() 4 5# 必要ライブラリのインポート 6from selenium import webdriver 7from selenium.webdriver.common.by import By 8from selenium.webdriver.common.keys import Keys 9import chromedriver_binary 10import time 11import pandas as pd 12 13from selenium.webdriver.support.ui import WebDriverWait 14from selenium.webdriver.support import expected_conditions as EC 15 16#ブラウザの設定 17options = webdriver.ChromeOptions() 18options.add_argument('--headless') 19options.add_argument('--no-sandbox') 20options.add_argument('--disable-dev-shm-usage') 21 22#ブラウザの起動 23browser = webdriver.Chrome(options=options) 24wait = WebDriverWait(browser, 3) 25 26url = 'https://jp.mercari.com/item/m66669659367' 27 28browser.get(url) 29time.sleep(5) 30 31#要素取得 32element = browser.find_element(By.CSS_SELECTOR, '#item-info > section:nth-child(1) > section:nth-child(3) > div > pre:nth-child(2)').text 33#element = browser.find_element(By.CSS_SELECTOR, '#item-info > section:nth-child(1) > div.mer-spacing-b-12 > div > div > h1').text 34print('売れてからの経過時間 : ' + element)
試したこと
そのままセレクターをコピーすると
python
1'#item-info > section:nth-child(1) > section:nth-child(3) > div > pre.merText.small__5616e150.primary__5616e150.bold__5616e150'
ですが、2連続アンダースコアが入っているため、nth-childで置き換えましたが、取得ができませんでした
同じような要素の構成が数か所あり、どこも取得する同階層にshadow_rootがあります。取得したい要素自体はshadow_root内ではないので、なぜ拾えないのかわかりません
pythonを触って2日目なので、調べ足りないのかもしれませんが、お力をお貸しいただければと思います
補足情報(FW/ツールのバージョンなど)
項目 | バージョン |
---|---|
Windows11 | 22H2(22621.2428) |
Python | 3.12.0 |
selenium | 4.14.0 |
Chrome | 118.0.5993.89 |
> 一部の要素がどうしても抜けません
どこまで取得できているのか確認してはどうでしょうか?
ありがとうございます。
ソースでコメントアウトしている要素で実行するとちゃんとprintされるのですが、本質問の要素では以下のエラーが発生します
セレクターで要素が見つからないような感じです
Traceback (most recent call last):
File "C:\Users\XXXX\Desktop\python_learn\XXXX.py", line 32, in <module>
element = browser.find_element(By.CSS_SELECTOR, '#item-info > section:nth-child(1) > section:nth-child(3) > div > pre:nth-child(2)').text
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXXX\AppData\Local\Programs\Python\Python312\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\XXXXX\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 344, in execute
self.error_handler.check_response(response)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python312\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":"css selector","selector":"#item-info > section:nth-child(1) > section:nth-child(3) > div > pre:nth-child(2)"}
(Session info: headless chrome=118.0.5993.89); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
> どこまで取得できているのか確認してはどうでしょうか?
上記はやりましたか?”どこまで取得できているのか”を地道に確認するしかないかと思います。
申し訳ございません。
どこまで取得できているのかという意味が分かりません。。。
同じソースで別の要素はprintされて、本質問の要素は上記エラーとなり取得ができないため、どこまで取得かどうかについてトレースのしかたなど、参考になる手法があればご教授ください
よろしくお願いいたします
> '#item-info > section:nth-child(1) > section:nth-child(3) > div > pre:nth-child(2)'
上記でエラーになるなら'#item-info > section:nth-child(1) > section:nth-child(3) > div 'ではエラーになるのか?ならないのか?のように要素を遡って調べてみる、ということです。

回答1件
あなたの回答
tips
プレビュー