我正在尝试使用 Capybara 找到一个不可见的字段来填充,但我所做的一切似乎都不起作用

I'm trying to find an invisible field to fill in using Capybara, but nothing I am doing seems to work

我目前正在尝试使用 Capybara 填写 Google 登录表单,但我很难找到要填写的隐藏字段。

这是我要填写的字段的HTML。

这里是测试代码

require "rails_helper.rb"

RSpec.describe "Sign in page" do
    it 'displays the page intention' do
        visit('/mars')
        expect(page).to have_content 'Sign in with Google'
    end
    it 'fills in user information' do
       find(:xpath, '//*[@id="identifierId"]', visible: false)
    end
end

这就是我遇到的错误。

首先,您不应该尝试填写隐藏字段,您应该像用户必须的那样与可见元素进行交互。在这种情况下,这可能意味着需要先点击另一个元素来触发电子邮件输入变为活动状态。像

first('form div[jscontroller]').click
fill_in('identifierId', with: email)

应该适用于 google 登录页面。

注意:您还应该尽可能使用 CSS 而不是 XPath 来查找元素,因为它会更快、更易于阅读,并且不会无意中破坏元素作用域(// vs .// 在 XPath 中 - 参见 https://github.com/teamcapybara/capybara#beware-the-xpath--trap)

更新:此外,所有 it 块都是完全独立的,中间会重置浏览器,因此您需要为每个块访问所需的页面(如果所有 it 块都在describe 需要访问同一页面,您可以将访问放在 before 块中。

注意:这一切都假定您实际使用 Selenium 驱动程序进行此测试,正如问题标签所暗示的那样。