Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id of an element when using Capybara for test (Rails)

I am using Capybara to write test in my application, but now i have a situation in which i need to read id of an element within capybara like

myid = page.find("#parentNode").first(".childClass").id

Consider i have the below HTML structure

<div id="parentNode">
 <div id="childNode1" class="childClass">1</div>
 <div id="childNode2" class="childClass">2</div>
</div>

Please Note : I am not trying to read the content of the child node, but the id. The above shown is for example.

Expected Output : childNode1 (id of first element with class childClass

like image 970
balanv Avatar asked Dec 06 '12 09:12

balanv


1 Answers

You are almost near the answer. The only change is instead of calling id as method, you have to call it as attribute as follows

page.find("#parentNode").first(".childClass")[:id]
like image 65
shivashankar Avatar answered Sep 23 '22 13:09

shivashankar