joe chesak
2010-Jun-13 09:05 UTC
[Mechanize-users] populating & submitting a form with a ''node'' (repost. fixed an important typo)
Hello,
I am trying to populate a username and password, then submit the form.
I''ll
post the form object from mechanize and then show how I am doing.
=> #<Mechanize::Form
{name "Form1"}
{method "POST"}
{action "default.aspx"}
{fields
#<Mechanize::Form::Hidden:0x103ae63a8
@name="__VIEWSTATE",
@node #(Element:0x81d734a4 {
name = "input",
attributes = [
#(Attr:0x81d70498 { name = "type", value = "hidden"
}),
#(Attr:0x81d70484 { name = "name", value =
"__VIEWSTATE" }),
#(Attr:0x81d70470 { name = "id", value =
"__VIEWSTATE" }),
#(Attr:0x81d7045c {
name = "value",
value
"/wEPDwUKLTk5MDMzOTQ2OA9kFgICAQ9kFgoCAQ8WAh4Fd2lkdGgFBTc2OXB4FgJmD2QWAmYPZBYCAgEPDxYCHgtOYXZpZ2F0ZVVybAUMZGVmYXVsdC5hc3B4ZGQCAw8WAh8ABQU3NjlweGQCBQ8WAh8ABQU3NjlweBYCZg9kFgICAg9kFgICAw9kFgICBw9kFhICAQ8PFgIeBFRleHQFCjEzLjA2LjIwMTBkZAIFDw8WAh8CBQg5MzfCoDEzOWRkAgkPDxYCHwIFCDM4NsKgNjk1ZGQCDg8PFgIeB1Zpc2libGVoZGQCEA8PFgIfAgUBMGRkAhMPDxYCHwNoZGQCFQ8PFgIfAgUBMGRkAhgPDxYCHwNoZGQCGg8PFgIfAgUBMGRkAgcPFgIfAAUFNzY5cHhkAgkPFgIfAAUFNzY5cHhkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRBfY3RsMTg6aWJ0bkxvZ2luBRNfY3RsMTg6Y2JSZW1lbWJlck1l0mXXF4LaeFCK0sHzC+ps+KkutyE="
})]
}),
@value
"/wEPDwUKLTk5MDMzOTQ2OA9kFgICAQ9kFgoCAQ8WAh4Fd2lkdGgFBTc2OXB4FgJmD2QWAmYPZBYCAgEPDxYCHgtOYXZpZ2F0ZVVybAUMZGVmYXVsdC5hc3B4ZGQCAw8WAh8ABQU3NjlweGQCBQ8WAh8ABQU3NjlweBYCZg9kFgICAg9kFgICAw9kFgICBw9kFhICAQ8PFgIeBFRleHQFCjEzLjA2LjIwMTBkZAIFDw8WAh8CBQg5MzfCoDEzOWRkAgkPDxYCHwIFCDM4NsKgNjk1ZGQCDg8PFgIeB1Zpc2libGVoZGQCEA8PFgIfAgUBMGRkAhMPDxYCHwNoZGQCFQ8PFgIfAgUBMGRkAhgPDxYCHwNoZGQCGg8PFgIfAgUBMGRkAgcPFgIfAAUFNzY5cHhkAgkPFgIfAAUFNzY5cHhkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRBfY3RsMTg6aWJ0bkxvZ2luBRNfY3RsMTg6Y2JSZW1lbWJlck1l0mXXF4LaeFCK0sHzC+ps+KkutyE=">
#<Mechanize::Form::Text:0x103ae5f48
@name="_ctl18:txtUsername",
@node #(Element:0x81d732d8 {
name = "input",
attributes = [
#(Attr:0x81d6c244 { name = "name", value =
"_ctl18:txtUsername" }),
#(Attr:0x81d6c230 { name = "type", value = "text"
}),
#(Attr:0x81d6c21c { name = "id", value =
"_ctl18_txtUsername" }),
#(Attr:0x81d6c208 { name = "class", value =
"Textbox_red" })]
}),
@value="">
#<Mechanize::Form::Field:0x103ae5b10
@name="_ctl18:txtPassword",
@node #(Element:0x81d730d0 {
name = "input",
attributes = [
#(Attr:0x81d682ac { name = "name", value =
"_ctl18:txtPassword" }),
#(Attr:0x81d68298 { name = "type", value =
"password" }),
#(Attr:0x81d68284 { name = "id", value =
"_ctl18_txtPassword" }),
#(Attr:0x81d68270 { name = "class", value =
"Textbox_red" })]
}),
@value="">}
{radiobuttons}
{checkboxes
#<Mechanize::Form::CheckBox:0x103ae5160
@checked=false,
@name="_ctl18:cbRememberMe",
@value=nil>}
{file_uploads}
{buttons
#<Mechanize::Form::Submit:0x103ae57f0
@name="_ctl18:Button1",
@node #(Element:0x81d72ec8 {
name = "input",
attributes = [
#(Attr:0x81d60d2c { name = "type", value = "submit"
}),
#(Attr:0x81d60d18 { name = "name", value =
"_ctl18:Button1" }),
#(Attr:0x81d60d04 { name = "value", value = "Logg
inn" }),
#(Attr:0x81d60cf0 { name = "id", value =
"_ctl18_Button1" }),
#(Attr:0x81d60cdc { name = "class", value =
"LoginButton" }),
#(Attr:0x81d60cc8 {
name = "onmouseover",
value = "style.color=''#a3a3a3''"
}),
#(Attr:0x81d60cb4 {
name = "onmouseout",
value = "style.color=''White''"
})]
}),
@value="Logg inn">
#<Mechanize::Form::ImageButton:0x103ae5458
@name="_ctl18:ibtnLogin",
@node #(Element:0x81d72c98 {
name = "input",
attributes = [
#(Attr:0x81d5a8dc { name = "type", value = "image"
}),
#(Attr:0x81d5a8c8 { name = "name", value =
"_ctl18:ibtnLogin" }),
#(Attr:0x81d5a8b4 { name = "id", value =
"_ctl18_ibtnLogin" }),
#(Attr:0x81d5a8a0 { name = "src", value =
"images/right_arrow.GIF"
}),
#(Attr:0x81d5a88c { name = "border", value = "0" })]
}),
@value=nil,
@x=nil,
@y=nil>}>
First, I have trouble setting a values on "_ctl18:txtUsername" and
"_ctl18:txtPassword", maybe because of the strange characters in
there. So,
the way I am doing this form is like this...
form.fields[1] = "Roy"
form.fields[2] = "Rogers"
...after I do that the portion of the form for the login and password look
like this...
#(Element:0x81d732d8 {
name = "input",
attributes = [
#(Attr:0x81d6c244 { name = "name", value =
"_ctl18:txtUsername" }),
#(Attr:0x81d6c230 { name = "type", value = "text" }),
#(Attr:0x81d6c21c { name = "id", value =
"_ctl18_txtUsername" }),
#(Attr:0x81d6c208 { name = "class", value =
"Textbox_red" })]
}),
@value="Roy">
#<Mechanize::Form::Field:0x103ae5b10
@name="_ctl18:txtPassword",
@node #(Element:0x81d730d0 {
name = "input",
attributes = [
#(Attr:0x81d682ac { name = "name", value =
"_ctl18:txtPassword" }),
#(Attr:0x81d68298 { name = "type", value = "password"
}),
#(Attr:0x81d68284 { name = "id", value =
"_ctl18_txtPassword" }),
#(Attr:0x81d68270 { name = "class", value =
"Textbox_red" })]
}),
@value="Rogers">}
...It seems okay to me. I''m not sure about the ''node''
part but it seems to
be prepopulated. So I then submit the form...
form.submit
...but submitting this gives me the following error....
>> form.submit
NoMethodError: undefined method `node'' for "200628":String
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form/field.rb:29:in
`<=>''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:171:in
`sort''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:171:in
`build_query''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:221:in
`request_data''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:452:in
`post_form''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:370:in
`submit''
from
/Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:141:in
`submit''
from (irb):46
from :0
Am I not assigning the username and password to the correct variables?...
perh
Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/mechanize-users/attachments/20100613/ce445bcf/attachment.html>
joe chesak
2010-Jun-15 12:02 UTC
[Mechanize-users] populating & submitting a form with a ''node'' (repost. fixed an important typo)
Solved. Please close this thread. There are two variables in the hierarchy with the same name and I was assigning a value to the higher one, which was overwriting valuable information, such as ''node''. Joe On Sun, Jun 13, 2010 at 11:05 AM, joe chesak <mechanize at chesak.com> wrote:> Hello, > > I am trying to populate a username and password, then submit the form. > I''ll post the form object from mechanize and then show how I am doing. > > > => #<Mechanize::Form > {name "Form1"} > {method "POST"} > {action "default.aspx"} > {fields > #<Mechanize::Form::Hidden:0x103ae63a8 > @name="__VIEWSTATE", > @node> #(Element:0x81d734a4 { > name = "input", > attributes = [ > #(Attr:0x81d70498 { name = "type", value = "hid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kAhgPDxYCHwNoZGQCGg8PFgIfAgUBMGRkAgcPFgIfAAUFNzY5cHhkAgkPFgIfAAUFNzY5cHhkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRBfY3RsMTg6aWJ0bkxvZ2luBRNfY3RsMTg6Y2JSZW1lbWJlck1l0mXXF4LaeFCK0sHzC+ps+KkutyE="> > #<Mechanize::Form::Text:0x103ae5f48 > @name="_ctl18:txtUsername", > @node> #(Element:0x81d732d8 { > name = "input", > attributes = [ > #(Attr:0x81d6c244 { name = "name", value = "_ctl18:txtUsername" }), > #(Attr:0x81d6c230 { name = "type", value = "text" }), > #(Attr:0x81d6c21c { name = "id", value = "_ctl18_txtUsername" }), > #(Attr:0x81d6c208 { name = "class", value = "Textbox_red" })] > }), > @value=""> > #<Mechanize::Form::Field:0x103ae5b10 > @name="_ctl18:txtPassword", > @node> #(Element:0x81d730d0 { > name = "input", > attributes = [ > #(Attr:0x81d682ac { name = "name", value = "_ctl18:txtPassword" }), > #(Attr:0x81d68298 { name = "type", value = "password" }), > #(Attr:0x81d68284 { name = "id", value = "_ctl18_txtPassword" }), > #(Attr:0x81d68270 { name = "class", value = "Textbox_red" })] > }), > @value="">} > {radiobuttons} > {checkboxes > #<Mechanize::Form::CheckBox:0x103ae5160 > @checked=false, > @name="_ctl18:cbRememberMe", > @value=nil>} > {file_uploads} > {buttons > #<Mechanize::Form::Submit:0x103ae57f0 > @name="_ctl18:Button1", > @node> #(Element:0x81d72ec8 { > name = "input", > attributes = [ > #(Attr:0x81d60d2c { name = "type", value = "submit" }), > #(Attr:0x81d60d18 { name = "name", value = "_ctl18:Button1" }), > #(Attr:0x81d60d04 { name = "value", value = "Logg inn" }), > #(Attr:0x81d60cf0 { name = "id", value = "_ctl18_Button1" }), > #(Attr:0x81d60cdc { name = "class", value = "LoginButton" }), > #(Attr:0x81d60cc8 { > name = "onmouseover", > value = "style.color=''#a3a3a3''" > }), > #(Attr:0x81d60cb4 { > name = "onmouseout", > value = "style.color=''White''" > })] > }), > @value="Logg inn"> > #<Mechanize::Form::ImageButton:0x103ae5458 > @name="_ctl18:ibtnLogin", > @node> #(Element:0x81d72c98 { > name = "input", > attributes = [ > #(Attr:0x81d5a8dc { name = "type", value = "image" }), > #(Attr:0x81d5a8c8 { name = "name", value = "_ctl18:ibtnLogin" }), > #(Attr:0x81d5a8b4 { name = "id", value = "_ctl18_ibtnLogin" }), > #(Attr:0x81d5a8a0 { name = "src", value = "images/right_arrow.GIF" > }), > #(Attr:0x81d5a88c { name = "border", value = "0" })] > }), > @value=nil, > @x=nil, > @y=nil>}> > > > First, I have trouble setting a values on "_ctl18:txtUsername" and > "_ctl18:txtPassword", maybe because of the strange characters in there. So, > the way I am doing this form is like this... > > form.fields[1] = "Roy" > form.fields[2] = "Rogers" > > ...after I do that the portion of the form for the login and password look > like this... > > #(Element:0x81d732d8 { > name = "input", > attributes = [ > #(Attr:0x81d6c244 { name = "name", value = "_ctl18:txtUsername" }), > #(Attr:0x81d6c230 { name = "type", value = "text" }), > #(Attr:0x81d6c21c { name = "id", value = "_ctl18_txtUsername" }), > #(Attr:0x81d6c208 { name = "class", value = "Textbox_red" })] > }), > @value="Roy"> > #<Mechanize::Form::Field:0x103ae5b10 > @name="_ctl18:txtPassword", > @node> #(Element:0x81d730d0 { > name = "input", > attributes = [ > #(Attr:0x81d682ac { name = "name", value = "_ctl18:txtPassword" }), > #(Attr:0x81d68298 { name = "type", value = "password" }), > #(Attr:0x81d68284 { name = "id", value = "_ctl18_txtPassword" }), > #(Attr:0x81d68270 { name = "class", value = "Textbox_red" })] > }), > @value="Rogers">} > > ...It seems okay to me. I''m not sure about the ''node'' part but it seems to > be prepopulated. So I then submit the form... > > > form.submit > > ...but submitting this gives me the following error.... > > >> form.submit > NoMethodError: undefined method `node'' for "200628":String > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form/field.rb:29:in > `<=>'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:171:in > `sort'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:171:in > `build_query'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:221:in > `request_data'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:452:in > `post_form'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:370:in > `submit'' > from > /Users/joe/.gem/ruby/1.8/gems/mechanize-1.0.0/lib/mechanize/form.rb:141:in > `submit'' > from (irb):46 > from :0 > > > Am I not assigning the username and password to the correct variables?... > perh > > > Joe-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/mechanize-users/attachments/20100615/56c7b34c/attachment.html>