LoginSignup
hiroki2003
@hiroki2003

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

待機コマンドを入れてもStaleElementReferenceExceptionが出てしまう

解決したいこと

StaleElementReferenceExceptionのエラーを解消したいです。
time.sleep()を事前に入れているにもかかわらず、上記エラーが発生していまいます。
解決方法を教えて下さい。

発生している問題・エラー

StaleElementReferenceException 

該当するソースコード

yoyaku=driver.find_element(By.LINK_TEXT, "予約を確認する・申し込む")
yoyaku.click()

time.sleep(0.5)

driver.switch_to.frame(iframe)
driver.switch_to.frame("left")
favorite=driver.find_element(By.LINK_TEXT, "お気に入りを指定して検索")
favorite.click()

search=driver.find_element(By.CLASS_NAME, "IPTBTN")
search.click()

time.sleep(1)

iframe = driver.find_element(By.CSS_SELECTOR, "html > frameset > frame:nth-child(2)")
driver.switch_to.frame(iframe) #←ここでエラーが発生していまいます。iframeを使いまわしていることも関係あるのでしょうか?
driver.switch_to.frame("content")
date=driver.find_element(By.ID, "nextSpan")

time.sleep(5)
date.click()

該当のHTML

<img id="execNext" src="/reselve/IMAGES/pw2/p2_execNext.gif" title="手続きを進める" alt="次の手続き" onclick="doExecNext()" style="left: 441px; display: block;">
0

1Answer

StaleElementReferenceException は参照先の要素がすでにページから取り除かれていたときに発生します。待機を入れるかどうかは関係しません。

iframe を取得し直せば解決すると思います。

1

Comments

  1. @hiroki2003

    Questioner
    ご回答ありがとうございます。

    iframeを取得し直してみました。
    iframe = driver.find_element(By.CSS_SELECTOR, "html > frameset > frame:nth-child(2)")
    すると、今度は以下のエラーが出てしまいました。このiframeは別の箇所でも使用しており、別の箇所では正常に動いているので、誤っていないとは思うのですが。。。
    NoSuchElementException
  2. search.click() した後なのでページ遷移なりページの構造が変わるなりしたのでは?

Your answer might help someone💌