如何在 Mechanize 中选择此选择列表的选项
所以我这个代码运行得很好..最近..我需要选择一个选择列表的选项(只有一个具有 GET 方法)这是我正在使用的代码,
require 'mechanize'
require 'logger'
agent = Mechanize.new{|a| a.log = Logger.new(STDERR) }
agent.read_timeout = 60
def add_cookie(agent, uri, cookie)
uri = URI.parse(uri)
Mechanize::Cookie.parse(uri, cookie) do |cookie|
agent.cookie_jar.add(uri, cookie)
end
end
page = agent.get "http://www.webpage.com"
form = page.forms.first
form.correo_ingresar = "user"
form.password = "password"
page = agent.submit form
myarray = page.body.scan(/SetCookie\(\"(.+)\", \"(.+)\"\)/)
myarray.each{|line| add_cookie agent, 'http://www.sistemasaplicados.com.mx', "#{line[0]}=#{line[1]}"}
add_cookie(agent, 'http://www.webpage.com.mx', "tampag=1000; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigoseccion_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigolinea_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigomarca_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "textobuscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "orden_articulos=existencias asc; path=/; domain=www.sistemasaplicados.com.mx")
page = agent.get "http://www.webpage.com.mx/tienda/articulos.php"
busqueda = page.forms.first
resultado = agent.submit busqued
tamanio = resultado.form_with(:method => "GET")
oneselect = resultado.form_with(:name => "tamano")
所以我已经使用变量 tamanio 来显示该选择列表表单:
#<Mechanize::Form
{name nil}
{method "GET"}
{action "http://www.webpage.com.mx/tienda/articulos.php"}
{fields
#<Mechanize::Form::SelectList:0x2233350
@name="tamano",
@node=
#(Element:0x11199d8 {
name = "select",
attributes = [
#(Attr:0x10f30c8 {
name = "style",
value = "font-family: verdana; font-size: 10px; color:black;"
}),
#(Attr:0x10f30bc { name = "name", value = "tamano" }),
#(Attr:0x10f30b0 {
name = "onchange",
value = "if (confirm('Haz solicitado modificar la cantidad de articulos por p\u00E1gina. Este cambio permanecer\u00E1 hasta que cierres la sesi\u00F3n o solicites el cambio nuevamente. \u00BFDeseas continuar?')){SetCookie('tampag',this.value); document.location='/tienda/articulos.php';} else {return;} "
}),
#(Attr:0x10f30a4 {
name = "title",
value = "Cambiar cantidad de art\u00EDculos por p\u00E1gina"
})],
children = [
#(Element:0x11197e0 {
name = "option",
attributes = [
#(Attr:0x10f1a0c { name = "value", value = "10" }),
#(Attr:0x10f1a00 { name = "selected", value = "selected" })],
children = [ #(Text "10")]
}),
#(Element:0x1119780 {
name = "option",
attributes = [ #(Attr:0x10f0a40 { name = "value", value = "20" })],
children = [ #(Text "20")]
}),
#(Element:0x1119720 {
name = "option",
attributes = [ #(Attr:0x10efed0 { name = "value", value = "30" })],
children = [ #(Text "30")]
}),
#(Element:0x11196c0 {
name = "option",
attributes = [ #(Attr:0x10ef354 { name = "value", value = "40" })],
children = [ #(Text "40")]
}),
#(Element:0x1119660 {
name = "option",
attributes = [ #(Attr:0x10ee73c { name = "value", value = "50" })],
children = [ #(Text "50")]
}),
#(Element:0x1119600 {
name = "option",
attributes = [ #(Attr:0x10eda04 { name = "value", value = "100" })],
children = [ #(Text "100")]
}),
#(Element:0x11195a0 {
name = "option",
attributes = [ #(Attr:0x10ecec4 { name = "value", value = "500" })],
children = [ #(Text "500")]
}),
#(Element:0x1119540 {
name = "option",
attributes = [ #(Attr:0x10ec360 { name = "value", value = "1000" })],
children = [ #(Text "1000")]
})]
}),
@options=[10, 20, 30, 40, 50, 100, 500, 1000],
@value=[]>}
{radiobuttons}
{checkboxes}
{file_uploads}
{buttons}>
看起来选择列表是一个嵌套表单,所以我尝试:
selectlist.value = selectlist.options.first.value (replacing selectlist with the variable tamanio)
还尝试向现有选择列表表单添加一个新节点:
oneselect.new("select","1000")
我在这里遗漏了什么吗?..
So I have this code running pretty well.. lately.. and I need to select an option of a select list (only one that has GET method) here is the code I am using
require 'mechanize'
require 'logger'
agent = Mechanize.new{|a| a.log = Logger.new(STDERR) }
agent.read_timeout = 60
def add_cookie(agent, uri, cookie)
uri = URI.parse(uri)
Mechanize::Cookie.parse(uri, cookie) do |cookie|
agent.cookie_jar.add(uri, cookie)
end
end
page = agent.get "http://www.webpage.com"
form = page.forms.first
form.correo_ingresar = "user"
form.password = "password"
page = agent.submit form
myarray = page.body.scan(/SetCookie\(\"(.+)\", \"(.+)\"\)/)
myarray.each{|line| add_cookie agent, 'http://www.sistemasaplicados.com.mx', "#{line[0]}=#{line[1]}"}
add_cookie(agent, 'http://www.webpage.com.mx', "tampag=1000; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigoseccion_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigolinea_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "codigomarca_buscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "textobuscar=; path=/; domain=www.webpage.com.mx")
add_cookie(agent, 'http://www.webpage.com.mx', "orden_articulos=existencias asc; path=/; domain=www.sistemasaplicados.com.mx")
page = agent.get "http://www.webpage.com.mx/tienda/articulos.php"
busqueda = page.forms.first
resultado = agent.submit busqued
tamanio = resultado.form_with(:method => "GET")
oneselect = resultado.form_with(:name => "tamano")
so I already used variable tamanio to show that select list form:
#<Mechanize::Form
{name nil}
{method "GET"}
{action "http://www.webpage.com.mx/tienda/articulos.php"}
{fields
#<Mechanize::Form::SelectList:0x2233350
@name="tamano",
@node=
#(Element:0x11199d8 {
name = "select",
attributes = [
#(Attr:0x10f30c8 {
name = "style",
value = "font-family: verdana; font-size: 10px; color:black;"
}),
#(Attr:0x10f30bc { name = "name", value = "tamano" }),
#(Attr:0x10f30b0 {
name = "onchange",
value = "if (confirm('Haz solicitado modificar la cantidad de articulos por p\u00E1gina. Este cambio permanecer\u00E1 hasta que cierres la sesi\u00F3n o solicites el cambio nuevamente. \u00BFDeseas continuar?')){SetCookie('tampag',this.value); document.location='/tienda/articulos.php';} else {return;} "
}),
#(Attr:0x10f30a4 {
name = "title",
value = "Cambiar cantidad de art\u00EDculos por p\u00E1gina"
})],
children = [
#(Element:0x11197e0 {
name = "option",
attributes = [
#(Attr:0x10f1a0c { name = "value", value = "10" }),
#(Attr:0x10f1a00 { name = "selected", value = "selected" })],
children = [ #(Text "10")]
}),
#(Element:0x1119780 {
name = "option",
attributes = [ #(Attr:0x10f0a40 { name = "value", value = "20" })],
children = [ #(Text "20")]
}),
#(Element:0x1119720 {
name = "option",
attributes = [ #(Attr:0x10efed0 { name = "value", value = "30" })],
children = [ #(Text "30")]
}),
#(Element:0x11196c0 {
name = "option",
attributes = [ #(Attr:0x10ef354 { name = "value", value = "40" })],
children = [ #(Text "40")]
}),
#(Element:0x1119660 {
name = "option",
attributes = [ #(Attr:0x10ee73c { name = "value", value = "50" })],
children = [ #(Text "50")]
}),
#(Element:0x1119600 {
name = "option",
attributes = [ #(Attr:0x10eda04 { name = "value", value = "100" })],
children = [ #(Text "100")]
}),
#(Element:0x11195a0 {
name = "option",
attributes = [ #(Attr:0x10ecec4 { name = "value", value = "500" })],
children = [ #(Text "500")]
}),
#(Element:0x1119540 {
name = "option",
attributes = [ #(Attr:0x10ec360 { name = "value", value = "1000" })],
children = [ #(Text "1000")]
})]
}),
@options=[10, 20, 30, 40, 50, 100, 500, 1000],
@value=[]>}
{radiobuttons}
{checkboxes}
{file_uploads}
{buttons}>
Looks like the select list is a nested form so, I tried:
selectlist.value = selectlist.options.first.value (replacing selectlist with the variable tamanio)
also tried adding a new node to the existing selectlist form with:
oneselect.new("select","1000")
am I missing something here?..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您总体上感到困惑,对于答案,我只会给您一些注释并希望它有所帮助:
您可以通过调用表单上的方法来访问 SelectList
一个元素:
当表单的方法是 GET 时,通常更容易构造
url 并使用
agent.get
I think you're confused in general, for an answer I'll just give you some notes and hope it helps:
You can access a SelectList by calling a method on the form that it's
an element of:
When a form's method is GET, it's often easier to just construct the
url and use
agent.get