6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ubuntu Server + ruby + firefox webdriver

Last updated at Posted at 2016-01-31

GUIのないサーバ上でSeleniumを実行

ガンガン既出の情報ですが…。
銀行口座の情報を自動で連携するためにやってみました。(OFXで連携)

環境情報

Ubuntu 12.04
ruby 2.0.0

サーバではRails 4.0が稼働中

エラー

CUIのみのサーバーなので実行しようとするともちろん

Selenium::WebDriver::Error::WebDriverError: unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/firefox/launcher.rb:90:in `connect_until_stable'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/firefox/launcher.rb:55:in `block in launch'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/common/socket_lock.rb:43:in `locked'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/firefox/launcher.rb:51:in `launch'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/firefox/bridge.rb:43:in `initialize'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/common/driver.rb:53:in `new'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver/common/driver.rb:53:in `for'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.50.0/lib/selenium/webdriver.rb:86:in `for'
	from /home/user/rails/hal.mu/app/controllers/concerns/download_ofx.rb:20:in `execute'
	from (irb):2
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
	from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

こんな感じのエラーに…。

必要なもの

仮想のディスプレイとして働く xvfb をインストールします。

sudo apt-get install xvfb -y

さらに、いちいち起動するわけにもいかないので headless もインストール

gem install headless

実行するソース

三井住友銀行 SMBCダイレクトにログインしたうえで一つの口座のOFXファイルをダウンロードします。
ログインと口座選択に必要な情報はDBから取得するようにしています。

app/controllers/concerns/download_ofx.rb
require "selenium-webdriver"

module DownloadOfx
                                                                                                                                                                    
  def execute
    @headless = Headless.new(dimensions: "1920x1080x24")
    @headless.start
    # headlessはデフォルトで99
    `export DISPLAY=:99`
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['intl.accept_languages'] = "ja"
    profile['general.useragent.locale'] = "ja-JP"
    profile['browser.download.dir'] = "/tmp/selenium-webdriver"
    profile['browser.download.folderList'] = 2
    profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/x-ofx'
    @driver = Selenium::WebDriver.for :firefox, :profile => profile
    @base_url = "https://direct.smbc.co.jp"
    @accept_next_alert = true
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []

    # 情報をDBから取得
    # ログインID 前半
    bank_account_userid1 = ConstantValue.where(:key => 'bank_account_userid1').first.value
    # ログインID 後半
    bank_account_userid2 = ConstantValue.where(:key => 'bank_account_userid2').first.value
    # 暗証番号
    bank_account_pin = ConstantValue.where(:key => 'bank_account_pin').first.value
    # 口座リンクのラベルテキスト
    bank_account_label = ConstantValue.where(:key => 'bank_account_label').first.value

    # ログイン画面を開く
    @driver.get(@base_url + "/aib/aibgsjsw5001.jsp")
    @driver.find_element(:id, "USRID1").clear
    @driver.find_element(:id, "USRID1").send_keys bank_account_userid1
    @driver.find_element(:id, "USRID2").clear
    @driver.find_element(:id, "USRID2").send_keys bank_account_userid2
    @driver.find_element(:id, "PASSWORD").clear
    @driver.find_element(:id, "PASSWORD").send_keys bank_account_pin
    @driver.find_element(:name, "bLogon.y").click
    # 口座を開く
    @driver.find_element(:link, bank_account_label).click
    # OFXファイルのダウンロード
    @driver.find_element(:id, "DownloadOFX").click
    @driver.find_element(:link, "ログアウト").click
    @driver.find_element(:css, "input.formButtonNavi2.leftButton").click

    @driver.quit
    @headless.destroy
  end
end

これで無事にダウンロードできるようになりました。

6
7
0

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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?