Chints Vadgama February 2016

How can I get Title & href (URL)?

I have following HTML code. I want to get the href & title of the product & store them into different variables. I have tried following code.

within("div.product-action") do
        @product_url = find("a.href")
      end

But that throws an error.

Capybara::ElementNotFound: Unable to find css "a.href"

My HTML code is as follow:

<div class="product-action zoom" ng-class="{ 'logged-out': !user.$isLoggedIn }">
   <a href="/g/women/christian-dior/so-real-sunglasses-colorless" title="Christian Dior So Real&quot; Sunglasses-Colorless" ng-click="ProductUtils.cache(result)" class="bottom-action-container quickview-button hover-option" track="{
                type: 'product-info',
                name: moduleType,
                breadcrumbs: result.breadcrumbs || breadcrumbs
              }">
              <i class="icon-zoom"></i>
    </a>
</div>

Answers


yenshirak February 2016

a.href will select the a elements that have a href class. This is not what you want.

You can access the attributes as a hash after you found the element:

a = find('.product-action a')
href = a[:href]
title = a[:title]


Sarabjit Singh February 2016

You can find the href and title of given html code with below mentioned code:

within("div.product-action") do
 productUrl = find(:css, '.bottom-action-container')[:href]
 productTitle = find(:css, '.bottom-action-container')[:title]
end

OR

within("div.product-action") do
 productUrl = find('a')[:href]
 productTitle = find('a')[:title]
end

Hope this helps :)

Post Status

Asked in February 2016
Viewed 1,173 times
Voted 5
Answered 2 times

Search




Leave an answer