1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Selenium ActionChainsでStaleElementReferenceExceptionが発生する原因と対策 要検証

Last updated at Posted at 2019-06-23

はじめに

Seleniumを使っていると、StaleElementReferenceExceptionが発生しました。
だたし、操作対象のelementは直前に取得しており、なおかつ表示されているので存在していないとは考えられませんでした。
そこで、自分が変更した個所と動作を検証した結果、原因らしきものが分かりました。
もっとも、原因に関しては個人の予想の範疇なので間違っているかもしれません。

例外が発生する記述

単純化すると以下のような記述の場合に発生します。

python
actions = ActionChains(driver)

element = driver.find_element(By.ID, "1")
actions.move_to_element(element).perform()

# 何かの処理
# その結果、IDが"1"のelementの参照が切れる

element = driver.find_element(By.ID, "2")
actions.move_to_element(element).perform()

この記述だと、2度目のactions.move_to_element(element).perform()でStaleElementReferenceExceptionが発生するはずです。
なぜならば、move_to_elementの出発点がidが"1"のelementのためにその参照が必要なためと思われます。

もしくは、以下のようにしても例外が発生すると思います。
actions.click(element)は、内部でactions.move_to_element(element)を呼んでいるからです。

python
actions = ActionChains(driver)

element = driver.find_element(By.ID, "1")
actions.click(element).perform()

# 何かの処理
# その結果、IDが"1"のelementの参照が切れる

element = driver.find_element(By.ID, "2")
actions.click(element).perform()

修正方法

上記の記述で問題だったのは、ActionChainsのインスタンスを使いまわしてしまったことです。
これを避けるためには、インスタンスを作り直せば良いだけです。

python
actions = ActionChains(driver)
element = driver.find_element(By.ID, "1")
actions.move_to_element(element).perform()

# 何かの処理
# その結果、IDが"1"のelementの参照が切れる

actions = ActionChains(driver)
element = driver.find_element(By.ID, "2")
actions.move_to_element(element).perform()

最後に

わざわざインスタンス作り直す必要ないだろうと横着しようとしたら思わぬエラーにぶつかりました。
適当な事をしては駄目という事でしょうか。
とりあえず対策できて一安心です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?