narzero
narzero

Reputation: 2299

How do I fill the value of a dom node input with Mechanize?

How do I fill the value of a dom node input with Mechanize?

I want the website to show a range of trucks manufactured from 2010 upwards so I can parse the relevant information.

There is no actual form involved.

This is the code:

require "mechanize"

@url = "https://www.kleyntrucks.nl/vrachtwagens/trekker/"

a = Mechanize.new do |agent|
  agent.user_agent_alias = 'Mac Safari'
  agent.follow_meta_refresh = true
end

a.get(@url) do |page|

    bouwjaar_range_field = page.search("#imprp0")

    bouwjaar_range_details = bouwjaar_range_field.search(".details")

    input = bouwjaar_range_details.search("input")[0]

    input.value = "2010"

end

This is the output:

/Users/username/Dropbox/Development/Rails/folder/lib/tasks/experiment.rb:20:in `block in <main>': undefined method `value=' for #<Nokogiri::XML::Element:0x007fd4ab0fd9a8> (NoMethodError)
    from /Users/username/.rvm/gems/ruby-2.0.0-p353@global/gems/mechanize-2.7.3/lib/mechanize.rb:442:in `get'
    from /Users/username/Dropbox/Development/Rails/folder/lib/tasks/experiment.rb:10:in `<main>'
[Finished in 1.9s with exit code 1]

pp input gives me the following output:

#(Element:0x3fe33c82ab68 {
  name = "input",
  attributes = [
    #(Attr:0x3fe33c869430 { name = "type", value = "hidden" }),
    #(Attr:0x3fe33c8692a0 { name = "name", value = "slider_from" }),
    #(Attr:0x3fe33c869278 { name = "value", value = "1968" })]
  })

puts input gives me the following output:

<input type="hidden" name="slider_from" value="1968">

Any ideas?

Update

What I want it to do:

  1. Visit https://www.kleyntrucks.com/trucks/tractorunit/
  2. Set "MATRICULATION YEAR" (iow year of manufacture) to the range of 2010 and 2014
  3. Scrape information about all the trucks that are listed (and are manufactured between 2010 and 2014)

Upvotes: 0

Views: 1096

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

Change the line input.value = "2010"

to

input['value'] = "2010"

type, name etc are the attributes of the input element. You need to use Nokogiri::XML::Node#[]= method for the same.

Read the documentation of []=(name, value)

Set the attribute value for the attribute name to value

Example :

require 'nokogiri'

doc = Nokogiri::XML <<-xml
<foo atr = '123'> test </foo>
xml

doc.at('foo')['atr'] # => "123"
doc.at('foo')['atr'] = "999"
puts doc

output :

<?xml version="1.0"?>
<foo atr="999"> test </foo>

Upvotes: 1

Related Questions