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

发布于 2024-12-14 06:43:25 字数 4350 浏览 3 评论 0原文

所以我这个代码运行得很好..最近..我需要选择一个选择列表的选项(只有一个具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-12-21 06:43:25

我认为您总体上感到困惑,对于答案,我只会给您一些注释并希望它有所帮助:

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

     select_list = form.field_with(:name => "tamano")
    
  • 当表单的方法是 GET 时,通常更容易构造
    url 并使用 agent.get

    url = "http://www.mysite.com/page?var=#{select_list.value}"
    代理.获取 url
    

I think you're confused in general, for an answer I'll just give you some notes and hope it helps:

  • There's no such thing as nested forms
  • A SelectList is not a form, it's a form element, or a form control
  • You can access a SelectList by calling a method on the form that it's
    an element of:

     select_list = form.field_with(:name => "tamano")
    
  • When a form's method is GET, it's often easier to just construct the
    url and use agent.get

    url = "http://www.mysite.com/page?var=#{select_list.value}"
    agent.get url
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文