python seleniumでエラー「’WebElement’ object is not iterable」が発生した場合の対処法

pythonで、selenium使用時にエラー「’WebElement’ object is not iterable」が発生した場合の対処法を記述してます。「selenium」のバージョン4.3以降は「find_element」を使用しますが、複数の場合は「find_elements」を使用します。これが原因です。
環境
- OS windows 11 Home
- python 3.10.8
- selenium 4.7.2
エラー全文
以下の、classを指定してループ処理しようとしたコードで発生
elem_sample = driver.find_element(By.CLASS_NAME,"classname")
for e in elem_sample:
print(e.text)
エラーメッセージ
例外が発生しました: TypeError
'WebElement' object is not iterable
File "C:\python\test.py", line 46, in <module>
for e in elem_sample:
TypeError: 'WebElement' object is not iterable
画像

原因
「find_element」は単一の要素を返すため
対処法
「find_elements」を使用します。
elem_sample = driver.find_elements(By.CLASS_NAME,"classname")
-
前の記事
java cosh(ハイパーボリックコサイン)値を取得する 2023.01.18
-
次の記事
google スプレッドシート 現在日付を入力するショートカットキー 2023.01.18
コメントを書く