我正在编写一个程序,使用Mechanize使用学生帐户从edline.net抓取学生的成绩和班级,并返回我需要的数据。但是,在登录后,我必须从主页访问一个链接(称为“私人报告'”),然后动态地返回一个链接页面到每个学生的班级和各自的成绩。
测试时,我创建了一个新对象my_account
,它有几个实例变量,包括主页。我将新变量指向实例变量,以便更简单地阅读):
result_page = agent.page.link_with(:text => 'Private Reports').click
我明白了:
Mechanize::UnsupportedSchemeError
但如果我要用:click
替换:text
,它会正确回复,而result_page
将等同于链接的文字"私人报告"
为什么它会正确回复:text
但是:click
会出错?有没有办法解决这个问题,还是应该重新考虑我对这个问题的解决方案?
以下是代码:
class AccountFetcher
EDLINE_LOGIN_URL = 'http://edline.net/Index.page'
def self.fetch_form(agent)
# uses @agent of an Account object
page = agent.get(EDLINE_LOGIN_URL)
form = page.form('authenticationEntryForm')
end
# edline's login form has to have several pre-conditions met before submitting the form
def self.initialize_form(agent)
form = AccountFetcher.fetch_form(agent)
submit_event = form.field_with(:name => 'submitEvent')
enter_clicked = form.field_with(:name => 'enterClicked')
ajax_support = form.field_with(:name => 'ajaxSupported')
ajax_support.value = 'yes'
enter_clicked.value = true
submit_event.value = 1
return form
end
# logs the user in and returns the homepage
def self.fetch_homepage(u_username, u_password, agent)
form = AccountFetcher.initialize_form(agent)
username = form.field_with(:name => 'screenName')
password = form.field_with(:name => 'kclq')
username.value = u_username
password.value = u_password
form.submit
end
end
# class Account will be expanded later on but here are the bare bones for a user to log in to their account
class Account
attr_accessor :report, :agent, :username, :password
def initialize(u_username, u_password)
@agent = Mechanize.new
@username = u_username
@password = u_password
end
def login
page = AccountFetcher.fetch_homepage(self.username, self.password, self.agent)
@report = page
end
end
my_account = Account.new('ex_username', 'ex_password')
my_account.login
page = my_account.report
agent = my_account.agent
page = agent.page.link_with(:text => 'Private Reports').click
答案 0 :(得分:0)
链接指向哪里?即,什么是href?
我问因为“scheme”通常指的是像http或https或ftp这样的东西,所以链接有一个奇怪的方案,机械化不知道如何处理,因此Mechanize::UnsupportedSchemeError