Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Mechanize中选择此选择列表的选项_Ruby_Nokogiri_Mechanize - Fatal编程技术网

Ruby 如何在Mechanize中选择此选择列表的选项

Ruby 如何在Mechanize中选择此选择列表的选项,ruby,nokogiri,mechanize,Ruby,Nokogiri,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.pars

所以我的代码运行得很好。。最近。。我需要从选择列表中选择一个选项(只有一个有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表单添加新节点:

oneselect.new("select","1000")

我是不是遗漏了什么

我想你总的来说有点困惑,为了得到答案,我只给你一些提示,希望能有所帮助:

  • 没有嵌套表单这样的东西
  • SelectList不是表单,它是表单元素或表单控件
  • 您可以通过调用SelectList所在表单上的方法来访问它 一个要素:

     select_list = form.field_with(:name => "tamano")
    
  • 当一个表单的方法被获取时,通常只需构造 url并使用
    agent.get

    url = "http://www.mysite.com/page?var=#{select_list.value}"
    agent.get url
    

@pguardiario我确实检查过了out@pguardiano,谢谢你现在的回答。很明显,我在“嵌套形式”上弄错了。我确实用那个字段搜索了一个表单。但上面写着“无”,即使有一个名为塔玛诺的选择列表。仍在试图找出原因。。。我不知道这是否是一个错误或可能是什么问题。
url = "http://www.mysite.com/page?var=#{select_list.value}"
agent.get url