24
17

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.

[RSpec] CapybaraでCSSの●番目の要素を指定したい!(同じ要素が複数あるとき)

Last updated at Posted at 2019-01-26

例えば

class: "fa-pen"が3個あるとき

find(".fa-pen").click

→Rails「何個目の".fa-pen"だよ??」

こうする

first(".fa-pen").click

→1個目を取り出す

2個目以降は??

second(".fa-pen").click

 Failure/Error: second(".fa-pen").click
     
     NoMethodError:
       undefined method `second' for #<RSpec::ExampleGroups〜〜〜

ダメじゃん。。
# 2個目以降は、【配列から取り出す】
page.all(".fa-pen")[1].click

→配列のindexが1なので、2個目を取り出す
(1個目を取り出す場合は、[0]とする)

#お世話になりました

参考:Capybara で同名の CSS セレクタを持つ複数の HTML 要素から任意の要素を見つける
https://easyramble.com/find-html-element-with-capybara.html


おわり
24
17
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
24
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?