无法通过 fill_in 找到字段“q”(Capybara::ElementNotFound)

Unable to find field “q” (Capybara::ElementNotFound) via fill_in

正在尝试从 indeed.com

获取 "what" 表格(又名职位)

尝试 运行 程序时出错:

/var/lib/gems/2.3.0/gems/capybara-2.11.0/lib/capybara/node/finders.rb:44:in `block in find': Unable to find field "q" (Capybara::ElementNotFound)

通过来自 indeed.com 的 firefox 检查元素产生:name="q"

<span class="inwrap">
<input class="input_text" maxlength="512" size="31" aria-labelledby="what_label_top hidden_colon what_label_bot" name="q" autocomplete="off" id="what">
</span>
<div style="width:250px"><!-- --></div>

与爬虫中的代码匹配:

def perform_search
# For indeed
fill_in 'q', :with => @skillset
fill_in 'l', :with => @region
find('#fj').click
sleep(1)
end

完整代码可在以下位置找到: https://github.com/jasnow/job-hunter/blob/master/scraper.rb

现在的问题是无法找到 name="q" 是否有任何其他方法可以 link 到 indeed.com 上的那个表格,以便我可以启动网络抓取?我说的可能是 xpath 或 css。

您的代码只允许 URL http://www.indeed.com ,但是 URL 重定向到 https://www.indeed.com 并且还命中 http://indeed.com。因此,您的页面加载被阻止。改成config.allow_url("indeed.com")应该可以找到输入的