Capybara :: ElementNotFound - 如何使用Capybara单击带有id的特定按钮

时间:2014-01-22 20:13:05

标签: ruby-on-rails selenium capybara

尝试在root url上访问此按钮:

enter image description here

这里是html

enter image description here

进行此测试:

feature "New comment button" do
  scenario "User can add new comment on root page", :js => true do
    visit root_path
    id = 152
    click_button("#button_#{id}")
    within("#comment_row_#{id}") do
        fill_in('content', :with => 'this is a comment')
        click_button('create comment')
        page.must_have_flash_message('Successfully created')
    end
  end

并提出这个问题:

Capybara::ElementNotFound: Unable to find button "#button_152"

如何使用id获取此元素?

我正在使用selenium-web-driver

修改

我做了什么

# page.driver.browser.switch_to.frame 'top-frame' # Selenium::WebDriver::Error::NoSuchFrameError: Unable to locate frame: top-frame
# page.find('#button_152').click          # not working
# click_button("#button_152")             # not working
# first(:xpath, '//button[@id="button_152"]').click

2.这是框架的概述:

enter image description here

  1. 所有iframe只是google chrome addons
  2. enter image description here

    4。link to full html

2 个答案:

答案 0 :(得分:0)

您可以在此处阅读有关切换窗口和框架的信息:http://docs.seleniumhq.org/docs/03_webdriver.jsp#moving-between-windows-and-frames

Ruby特定绑定:https://code.google.com/p/selenium/wiki/RubyBindings

处理iframe的Capybara:handling iframe with capybara ruby

至于你的问题,这里有一个你可以编辑的例子:

within_frame 'evernoteFilingTools' do
  click_button("#button_#{id}")
  #button_#{id} # not working 
  #page.find("#button_#{id}",:visible => true).click # does not work as well
  within("#comment_row_#{id}") do
    fill_in('content', :with => 'this is a comment')
    click_button('create comment')
    page.must_have_flash_message('Successfully created')
  end
end

您应该将evernoteFilingTools替换为包含您要操作的内容的iframe ID

答案 1 :(得分:0)

有时候,它可能会因无效标记而被抛弃。此HTML在表格中有div<div class="icons">),无效。尝试通过http://validator.w3.org/等验证程序运行标记,并修复它报告的任何错误。这也可能会解决你的水豚问题。