Css Capybara元素未找到错误,即使该元素存在

Css Capybara元素未找到错误,即使该元素存在,css,selenium,css-selectors,capybara,Css,Selenium,Css Selectors,Capybara,我正在用水豚草做硒测试,并试图填写一个弹出框。我无法使用capybara中的fill_in方法填充元素的名称。这与其他页面中的其他表单配合得很好,但在这一页上不行 以下是我的测试代码: describe "Jobs" do before do login(users(:admin)) end it "creates a job on a workspace" do visit('#/workspaces') click_button "Create Work

我正在用水豚草做硒测试,并试图填写一个弹出框。我无法使用capybara中的fill_in方法填充元素的名称。这与其他页面中的其他表单配合得很好,但在这一页上不行

以下是我的测试代码:

describe "Jobs" do
  before do
    login(users(:admin))
  end

  it "creates a job on a workspace" do
    visit('#/workspaces')
    click_button "Create Workspace"
    within_modal do
      fill_in 'name', :with => "testing-jobs"
      click_button "Create Workspace"
    end
    click_link "Dismiss the workspace quick start guide"
    page.should have_content('All Activity')
    Workspace.find_by_name("testing-jobs").should_not be_nil

    click_link "Jobs"
    click_button "Create"
    within('#facebox') do
      find(".name").click
      fill_in '. name', :with => "real job"
      choose "onDemand"
      click_button "Create"
    end
    page.should have_content "real job"
  end
end
关于工作空间的第一次填充非常好,但当我开始工作时,它就搞砸了

以下是firebug的实际开发代码:

<div id="facebox" class="dialog_facebox" style="top: 30px; left: 424.5px;">
<div class="popup" style="max-height: 626px;">
<div class="content">
<div class="configure_job_dialog dialog">
<div class="dialog_header">
<div class="errors"></div>
<div class="dialog_content" data-template="configure_job_dialog">
<form action="#">
<label class="required">Name</label>
<input class="name" type="text" value="">

名称
我在fill_in方法中使用name类,但水豚无法捕获它。我试着再调试一下,看看为什么创建了我的工作区,但没有创建作业。工作区代码如下所示

<input type="text" maxlength="256" tabindex="1" placeholder="Name this workspace" name="name">


非常感谢您的帮助。

问题

根据给定的html,您将无法使用
fill\u in
方法。原因是:

  • fill\u in
    方法根据元素的id属性、名称属性或标签文本定位元素。它不支持通过CSS选择器定位元素,CSS选择器是
    .name
  • 该元素没有id或name属性,因此不能由
    fill\u in
    使用。同样,标签也不会通过
    for
    id
    属性与输入直接关联。我相信
    fill\u in
    只检查显式标签(而不是隐式标签)
请注意,
fill_in
适用于工作区,因为该输入指定了name属性

解决方案

理想的解决方案是更新输入,使其具有id属性、name属性或显式标签。但是,如果不可能,或者需要使用CSS选择器,则可以
查找元素,然后
设置值:

find('.name').set("real job")

像做梦一样工作。谢谢你的建议!