10
7

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 3 years have passed since last update.

【RSpec】複数の要素から絞り込む/ 「Ambiguous match, found 2 elements matching」の解消

Posted at

概要

  • 複数の要素から一つの要素を絞り込む方法です
  • Ambiguous match, found 2 elements matching...の解消

該当のHTMLコード

<div class="actions">
  <input type="submit" name="back" value="修正" class="c-button-secondary col-4 offset-1" data-disable-with="送信中...">
  <input type="submit" name="commit" value="送信" class="c-button-primary col-4 offset-2" data-disable-with="送信中...">
</div>

RSpecコード

Before

find('input[type="submit"]').click

エラー文

Capybara::Ambiguous: Ambiguous match, found 2 elements matching visible css "input[type=\"submit\"]"

  0) Contact 問い合わせ完了の確認 未ログインの時 遷移及びメール通知が完了すること
     Failure/Error:[0mfind([31m[1;31m'[0m[31minput[type="submit"][1;31m'[0m[31m[0m)[[1;34m1[0m].click

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible css "input[type=\"submit\"]"



     # ./spec/system/contacts_spec.rb:22:in `block (4 levels) in <top (required)>'
     # -e:1:in `<main>'

After

find('input.offset-2').click

まとめ

  • 結局、固有のCSSクラス名を指定して解決した。
  • もし、複数の要素の中から最初の要素を指定する場合は、下記のようにすれば解決する
click_on 'hogehoge', match: :first

参考記事

10
7
2

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
10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?