这是我的Ruby Mechanize代码,并在最后评论,我坚持使用复选框,在页面上选择第二个提交,并在单击第二个提交按钮后显示一些字段。我已经使用了documentation并在周围搜索并找到了几种方法来复选框。其中一个可能正在工作,但我无法在表单中选择第二个提交并查看字段以查看。
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get(URL)
#Grab form
surescripts_form = page.form_with(:action => 'Login.aspx?timeout=true')
#Fill in login and password
surescripts_form.LoginIDTextBox = Username
surescripts_form.PasswordTextBox = Password
#Select the user profile form
surescripts_user_form = page.form_with(:name => 'form1')
FirstName = FirstNameVariable
LastName = LastNameVariable
DEA = DEANumberVariable
NPI = NPINumberVariable
surescripts_user_form['ctl00$CPHMainContent$TxtFirstName'] = FirstName
surescripts_user_form['ctl00$CPHMainContent$TxtLastName'] = LastName
surescripts_user_form['ctl00$CPHMainContent$TxtDEA'] = DEA
surescripts_user_form['ctl00$CPHMainContent$NPI'] = NPI
surescripts_user_form['ctl00$CPHMainContent$TxtAddressLine1'] = AddressLine1
#surescripts_user_form['ctl00$CPHMainContent$TxtAddressLine2'] = AddressLine2
surescripts_user_form['ctl00$CPHMainContent$TxtCity'] = City
surescripts_user_form['ctl00$CPHMainContent$DdlState$StateList'] = "New York"
surescripts_user_form['ctl00$CPHMainContent$TxtZip'] = Zip
surescripts_user_form['ctl00$CPHMainContent$TxtPrimaryPhone'] = PrimaryPhone
surescripts_user_form['ctl00$CPHMainContent$TxtFax'] = Fax
#surescripts_user_form['CPHMainContent_CblServiceLevel_1'] = 1
#surescripts_user_form['CPHMainContent_CblServiceLevel_2'] = 1
#Need help with this Check Box
#surescripts_user_form['CPHMainContent_CblServiceLevel_6'] = 1
#surescripts_user_form.checkbox_with(:value => '32').check
#surescripts_user_form.checkbox_with(:name => /CPHMainContent_CblServiceLevel_6/).check
#Need help with this Submit
#page = agent.submit(surescripts_user_form['ctl00$CPHMainContent$BtnSave'])
#page = surescripts_user_form.submit(page.form_with(:action => /.*/).submits[22])
page = agent.submit(surescripts_user_form, surescripts_user_form.buttons.last)
#Need help with displaying fields on the next page
pp page.body
pp surescripts_user_form
#Displays all my input values
File.open('form_log.txt', 'w') do |f|
# use "\n" for two lines of text
f.write surescripts_user_form.fields.each { |d| puts d.name }
end
#Displays html of the page
File.open('page_log.txt', 'w') do |f|
# use "\n" for two lines of text
f.write page.labels_hash()
end