実現したいこと
- Seleniumを用いてWebスクレイピングをしたい
 -何ページかに渡ってスクレイピングを実行したいが、”次のページ”が押されてページが遷移した後にエラーで止まる
 -ページ遷移後も止めずにスクレイピングを行いたい
前提
SeleniumでWebスクレイピングをしようしています。
何ページかに渡って情報を収集したいのですが、次のページに遷移した後にエラーでコードが止まってしまいます。
発生している問題・エラーメッセージ
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=109.0.5414.119)
該当のソースコード
Python3
1i = 0 2 3while True: 4 i = i +1 5 sleep(1) 6 keys = [] 7 8 for elem in elems_title: 9 elems_title = browser.find_elements_by_class_name("enHeadline") 10 key = elem.text 11 print(key) 12 keys.append(key) 13 14 next_elem = browser.find_element_by_class_name("nextItem") 15 next_elem.click() 16 sleep(5) 17 ##2週目がうまくいかないのはなぜか? 18 if i >5: 19 break
補足情報(FW/ツールのバージョンなど)
上記コードの”nextItem”が"次のページ"に該当します。
実行すると、当該ページの情報は収集しますが”次のページ”に遷移した後にエラーが出て止まります。
コードをどのように修正すれば止めずに収集できるでしょうか。
教えていただけると大変助かります。よろしくお願いいたします。
下記が出力とエラーの全体
veco Group NV - Iveco Group and Nikola Corporation’s sustainable transport journey progresses today at IAA Transportation 2022
QUANTRON to Unveil First Hydrogen Fuel Cell Truck Equipped With Allison eGen Power(R) Electric Axle at IAA
Iveco Group NV - IVECO and Hyundai present the first fuel cell large van at IAA in Hanover as their partnership develops
Iveco Group displays its product milestones towards net zero carbon mobility at IAA Transportation 2022
Press Release: Iveco Group displays its product milestones towards net zero carbon mobility at IAA Transportation 2022
StaleElementReferenceException            Traceback (most recent call last)
/var/folders/yy/g16v5qk95p3flj__2r5qpj_w0000gp/T/ipykernel_10005/2027160841.py in <module>
8     for elem in elems_title:
9         elems_title = browser.find_elements_by_class_name("enHeadline")
---> 10         key  = elem.text
11         print(key)
12         keys.append(key)
~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py in text(self)
74     def text(self):
75         """The text of the element."""
---> 76         return self._execute(Command.GET_ELEMENT_TEXT)['value']
77
78     def click(self):
~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
631             params = {}
632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
634
635     def find_element(self, by=By.ID, value=None):
~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319         response = self.command_executor.execute(driver_command, params)
320         if response:
--> 321             self.error_handler.check_response(response)
322             response['value'] = self._unwrap_value(
323                 response.get('value', None))
~/opt/anaconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240                 alert_text = value['alert'].get('text')
241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
243
244     def _value_or_default(self, obj, key, default):
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=109.0.5414.119)

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



バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/02/15 12:58