Capybara: is it possible to get an attribute value

2019-02-02 23:23发布

If I've got some HTML like:

<div id='drawer'><ul><li><a href='www'>description</a>...

I've seen that I can get the value of href in Capybara with:

page.find('div#drawer a')['href']

But is there any way to retrieve that value if it's completely described as a css string? ie, trying to get the value for 'div#drawer a[href]'

I've tried with expressions like this:

page.find('div#drawer a[href]')        => can't convert Capybara::Node::Element into String (TypeError)
page.find('div#drawer a[href]').value  => can't convert nil into String (TypeError)
page.find('div#drawer a[href]').text   => returns the text value of 'description'

I've got that css expression in an external config file, so would it be possible somehow to just use it directly and retrieve the value of the attribute in question?

Thank you...

标签: capybara
2条回答
Rolldiameter
2楼-- · 2019-02-02 23:50

Probably too late. But I also had the same problem and found the solution. It might help someone else.

page.find('div#drawer a')[:href]
查看更多
ら.Afraid
3楼-- · 2019-02-03 00:12

The only way I have been able to do this is with jQuery.

href = page.evaluate_script("$('a.link_class').attr('href');")
查看更多
登录 后发表回答