LoginSignup
0
1

More than 3 years have passed since last update.

Selenium::WebDriver::Error::StaleElementReferenceError

Posted at

開発環境

・Ruby: 2.6.3
・Rails: 6.0.3


Selenium::WebDriver::Error::StaleElementReferenceError

system specのテスト時に以下のエラーが出ました。


Selenium::WebDriver::Error::StaleElementReferenceError:
       stale element reference: element is not attached to the page document
         (Session info: chrome=83.0.4103.116)

ドキュメント

A command failed because the referenced element is no longer attached to the DOM.

訳:参照された要素がDOMにアタッチされなくなったため、コマンドが失敗しました。

エラーが起きたテストをみてみる

spec/system/users_spec.rb
#ログイン成功を確認するテスト
it 'is success that Creating new user with E-mail' do
      visit login_path
      fill_in 'E-mail', with: user.email, match: :first
      fill_in 'Password', with: 'suzuki1234'
      within '.login-form' do
      click_button 'Log in'
      expect(page).to have_content 'login success'
      expect(current_path).to eq user_path(user.id)
      end
    end

結論

spec/system/users_spec.rb
    within '.login-form' do
      click_button 'Log in'
      expect(page).to have_content 'login success'
      expect(current_path).to eq user_path(user.id)
      end

に問題があった。
view内に、Log inというテキストが二箇所あるので、範囲指定をするためにwithinを使っていたが、endの位置が悪かったみたい。

修正版

spec/system/users_spec.rb
it 'is success that Creating new user with E-mail' do
      visit login_path
      fill_in 'E-mail', with: user.email, match: :first
      fill_in 'Password', with: 'suzuki1234'
      within '.login-form' do
      click_button 'Log in'
      #ここにend
      end
      expect(page).to have_content 'login success'
      expect(current_path).to eq user_path(user.id)
    end

endの位置が悪かった。ごめんなさい...

0
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
0
1