我正在尝试使用mechanize gem从我的数据库向网站发布事件。我遇到的问题是许多必需的输入没有可用的名称。我已经尝试过各种方法通过css选择它们,但每次它们只返回nil。这是我的代码:
require 'mechanize'
#navigate to sign in page
puts "Running....."
agent = Mechanize.new { |agent|
agent.user_agent_alias = "Mac Safari"
}
page = agent.get('http://events.ctpost.com/home')
puts page.uri
link = page.link_with(text:'Add Event')
page = link.click
puts page.uri
#--------Enter sign in credentials----------
#enter email
form = page.forms.first
form['email'] = 'myemail'
#enter password
form = page.forms[1]
form['password'] = 'password'
page = form.submit
puts page.uri
pp page
#-------Create Event-------
#fill out event form
event_title = "House Show"
event_description = "House show at Joe's House"
event_venue = "Joes House"
event_date = "03/14/2018"
event_start_time = "7:00pm"
form = page.forms.first
form['title'] = event_title
form['description'] = event_description
form['price'] = "free"
form['phone'] = "666-666-6666"
form['event_venue'] = event_venue
这是我要选择的元素:
<input id="startTime" type="text" placeholder="* Start Time" class="required input-full time ui-timepicker-input" autocomplete="off">
是否可以通过id或其他css属性选择此项?我之前从未使用过机械。提前谢谢。
答案 0 :(得分:0)
试试这个
form.fields_with(:id => 'something').each do |field|
field.value = 'hello!'
end