Ruby 点击水豚

Ruby 点击水豚,ruby,selenium-webdriver,cucumber,capybara,Ruby,Selenium Webdriver,Cucumber,Capybara,我试图告诉水豚/黄瓜点击href,但尝试不同的选项失败。代码如下: <div> <h6 class="animation-visible tile-animation link-group color-white border-round bg-primary animated" data-animation-delay="1000" data-animation="cta-1"> <label>Option 1</label>

我试图告诉水豚/黄瓜点击href,但尝试不同的选项失败。代码如下:

  <div>
   <h6 class="animation-visible tile-animation link-group color-white border-round bg-primary animated" data-animation-delay="1000" data-animation="cta-1">
      <label>Option 1</label>
      <a href="http://some.com/page1/">More</a>
   </h6>
   <h6 class="animation-visible tile-animation link-group color-white border-round bg-primary animated" data-animation-delay="1000" data-animation="cta-1">
      <label>Option 2</label>
      <a href="http://some.com/page2/">More</a>
   </h6>
   ... 
</div>
那没用。我试着让它直接点击

find(all('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated>a')[0]).click 
那也没用

只是想知道是否有其他方法可以点击选项1?还是选择2?这些选项需要单独测试

这些不管用吗

第一环节

find_link("a[href$='page1/']").click
第二个环节:

find_link("a[href$='page2/']").click
css
选择器的翻译: 查找以XXX结尾的链接


假设元素在页面上是可见的(不确定h6元素上的动画类实际在做什么),那么您的第一次尝试将无法工作,因为在内部的选择器上有额外的>a-这意味着您已经找到了a,然后在内部说找到另一个a。您的第二次尝试将不起作用,因为find使用选择器类型和选择器(选择器类型默认为css)来匹配元素,但all返回一个元素数组(您可以调用find来返回一个元素,也可以调用all来返回多个元素)-以下操作应该可以替代

within(all('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated')[0]) do  
  find_link('More').click  
end
这是说里面的第一个。动画可见。。。匹配元素找到一个文本为“More”的链接,然后单击它。这也可以写出来

within(first('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated', minimum: 1)) do  
  click_link('More')  
end
这基本上是一样的,除了minimum:1选项将使它等待至少一个匹配的元素出现在页面上(如果您以前的调用是
#visit
,或者元素是通过ajax加载的,这很好) 注意:在第一个示例中,您也可以使用最小值:1(或您希望在页面上显示的任意数量的元素)

另一个答案中建议的
find_链接
调用无效,因为find_链接没有css选择器,它需要一个字符串来匹配链接的文本、id或标题

find_link('More', href: 'http://some.com/page1/').click


您遇到了什么异常?第一次尝试时,我发现
无法找到链接“更多”(Capybara::ElementNotFound
第二种方法是
无法找到链接(Capybara::ElementNotFound)
尝试在驱动程序实例化后添加隐式等待。类似于
driver.manage.timeouts.implicit_wait=3
谢谢。删除额外的>a实际上是有效的。我需要单击CSS中的所有
标记来执行测试。
find_link('More', href: 'http://some.com/page1/').click
click_link('More', href: 'http://some.com/page1/')