LoginSignup
10
11

More than 5 years have passed since last update.

Selenium#PhantomJSのユーザーエージェントを変える方法

Last updated at Posted at 2015-08-30

1.やりたいこと

RubyのSelenium(selenium-webdriver)のdriverをphantomjsにしてユーザーエージェントを変更する

2. Rubyスクリプト

require 'selenium-webdriver'

USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53"
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs('phantomjs.page.settings.userAgent' => USER_AGENT)
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities

3.動作確認

http://luftool.com/useragent/ に行ってスクリーンショットを撮ります
設定したユーザーエージェントになっていることが確認できます

driver.get('http://luftool.com/useragent/')
driver.save_screenshot('useragent.png')
driver.quit

1.png

4.参考

10
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11