所以我的代码运行得很好..最近..我需要选择一个选择列表的选项(只有一个有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")
我在这里错过了一些东西吗?..
答案 0 :(得分:1)
我觉得你总体上很困惑,我只会给你一些答案并希望它有所帮助:
您可以通过调用表单上的方法来访问SelectList 元素:
select_list = form.field_with(:name => "tamano")
当表单的方法是GET时,通常更容易构造
网址并使用agent.get
url = "http://www.mysite.com/page?var=#{select_list.value}"
agent.get url