我怎样才能获得Title& href(URL)?

时间:2016-02-08 21:05:15

标签: ruby-on-rails ruby capybara capybara-webkit

我有以下HTML代码。我想获得href&产品名称&将它们存储到不同的变量中我试过以下代码。

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

但是这会引发错误。

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

我的HTML代码如下:

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

2 个答案:

答案 0 :(得分:1)

a.href将选择具有a类的href元素。这不是你想要的。

您可以在找到元素后将属性作为哈希访问:

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

答案 1 :(得分:0)

您可以使用下面提到的代码找到给定html代码的href和标题:

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

希望这会有所帮助:)