4

I'm having difficulty setting the user-agent. As you can see my custom user_agent_alias is not being returned. Can someone please explain why this isn't working and how I can fix this?

require 'rubygems'
require 'mechanize'
require 'nokogiri'

m = Mechanize.new
m.user_agent_alias = 'My Custom User Agent'
page = m.get("http://whatsmyuseragent.com/")
html = Nokogiri::HTML(page.body)
puts html.xpath('//*[(@id = "body_lbUserAgent")]').map(&:content)

Below is the "user agent" being returned (not what I set):

Mechanize/2.7.3 Ruby/2.0.0p353 (http://github.com/sparklemotion/mechanize/)

2 Answers 2

5

Turns out that the issue was that user_agent_alias requires a specific type. All acceptable types are as follows:

  • Linux Firefox (3.6.1)
  • Linux Konqueror (3)
  • Linux Mozilla
  • Mac Firefox (3.6)
  • Mac Mozilla
  • Mac Safari (5)
  • Mac Safari 4
  • Mechanize (default)
  • Windows IE 6
  • Windows IE 7
  • Windows IE 8
  • Windows IE 9
  • Windows Mozilla
  • iPhone (3.0)
  • iPad
  • Android (Motorola Xoom)

Working code:

require 'rubygems'
require 'mechanize'

m = Mechanize.new
m.user_agent_alias = 'Mac Safari 4'
page = m.get("http://whatsmyuseragent.com/")
html = Nokogiri::HTML(page.body)
puts html.xpath('//*[(@id = "body_lbUserAgent")]').map(&:content)
Sign up to request clarification or add additional context in comments.

2 Comments

You can always retrieve the list of supported user_agent_aliases via: puts Mechanize::AGENT_ALIASES.keys. My installation returns ["Mechanize", "Linux Firefox", "Linux Konqueror", "Linux Mozilla", "Mac Firefox", "Mac Mozilla", "Mac Safari 4", "Mac Safari", "Windows Chrome", "Windows IE 6", "Windows IE 7", "Windows IE 8", "Windows IE 9", "Windows Mozilla", "iPhone", "iPad", "Android", "Mac FireFox", "Linux FireFox"] which is only slightly different from @Fab
OP's question was "...and how to fix". @severin answered how to fix it. Thumbsup for the explanation, though.
4

It is actually possible to set any user agent string: you have to use the method Mechanize::Agent#user_agent= instead of Mechanize::Agent#user_agent_alias=.

So if you change your example to:

m = Mechanize.new
m.user_agent = 'My Custom User Agent'
page = m.get("http://whatsmyuseragent.com/")

Then it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.