7
6

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.

Capybara で得た要素の innerHTML を取得する

Posted at

Capybara の find 使ってて、以下のようなエラーに遭遇することってありますよね。

> find('li.userList')
=> Capybara::Ambiguous: Ambiguous match, found 2 elements matching css "li.userList"

セレクタが曖昧で、2件ヒットしちゃったワケです。

ひとつめを取得するにはこうすればいいです。

> find_all('li.userList')[0]
=> #<Capybara::Node::Element tag="li" path="/html/body/div/div[2]/section/div[7]/div/div[1]/ul/li[1]">

とはいえその html がどういうふうになっているのか、やっぱ知りたいですよね。曖昧なセレクタは良くない。

というワケで、html を取得するには Capybara::Node::Element#native を使えば良いですよ。というハナシ。

> find_all('li.userList')[0].native.to_s
=> "<li class=\"userList\">\n< ……略……

参考にしたページ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?