久しぶりに Selenium のスクレイピングをしようと思ったら、エラーで動かなくなったのでその対処法。
環境
- Windows 11 Home 21H2
- Python 3.10.1
- Selenium 4.4.3
エラー
XPath を指定してエレメントを取得しようとしたら、次のエラーが発生。
1 | 'WebDriver' object has no attribute 'find_element_by_xpath' |
原因
Selenium 4.3.0 で削除されていた。
https://github.com/SeleniumHQ/selenium/blob/trunk/py/CHANGES#L24
1 | Deprecated find_element_by_* and find_elements_by_* are now removed |
解決方法
Selenium 4.3.0 以降は次のコードを使う。
1 | from selenium import webdriver # pip install selenium |