Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Capybara get element attribute value

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.91k
    Comment on it

    Many times we have to test the value of any attribute. With the help of find method, we can only locate the element but cannot get the value of all attributes of that element. So, to get the value of any attribute we have to give the name of that attribute inside the square '[]' bracket.

     

    Suppose, we have the following HTML:

    <div class="roommessage">
    <div>
    <div class="messagerow">
    <img src="/images/defaultprofileimage.jpg"/>
    <a class="heading" target="mainiframe" href="/profile/publicprofile/qatester1">qatester1</a>
    </div>
    This is dummy text
    </div>
    <div class="messageTime">
    </div>
    <div class="spaceForScroll" style="display: none;"/>
    </div>

     

    We have to get the value of source attribute of image tag

     

    Then(/^I should see the User Profile image and User ID in message section$/) do
      a = @session.find(:xpath, "//*[@id='c_73_conference_display']/div[last()-1]//div[@class='messagerow']/img")['src']
      puts "value of src attribute = #{a}
      self
    end

     

    So, as we can see in the above step definition that first we have located the element through find() method and then get the value of desired attribute by placing the attribute inside the square bracket after the find() method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: