Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara: is it possible to get an attribute value from a css string?

Tags:

capybara

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

like image 700
mickael Avatar asked Dec 17 '12 00:12

mickael


3 Answers

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]
like image 140
Leonardo Alves Avatar answered Nov 03 '22 18:11

Leonardo Alves


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

href = page.evaluate_script("$('a.link_class').attr('href');")
like image 33
RobertH Avatar answered Nov 03 '22 17:11

RobertH


If you are looking for an HTML element with a given value for a given attribute, you could do:

expect(page).to have_css('a[href="www"]')
like image 1
cdmo Avatar answered Nov 03 '22 18:11

cdmo