LoginSignup
19
18

More than 5 years have passed since last update.

Watir でIEやGoogle Chromeを自動操縦する

Last updated at Posted at 2012-06-09

Watir を使えば、IEやGoogle Chromeを自動操縦することができます。
Webサイトの自動操縦やテストの自動化など、色々と便利に使えそう。

Watirのインストール

下記記事の時と同じく、 msys.bat でコマンドプロンプトを起動した状態でコマンドを入力します。

gem update --system
gem install watir
gem install watir-webdriver

Watirを使って、Picasaウェブアルバムに自動ログイン

  • 一度Picasaウェブからログアウトしてから試してください
  • mail_address と password はご自分のものに置き換えてください
sample_autologin_picasaweb.rb
require "rubygems"
require "watir"         # for IE
#require "watir-webdriver"  # for Firefox/Chrome

# your gmail account
mail_address = "xxxxxxxx@gmail.com"
password = "password"

browser = Watir::IE.new
browser.goto "https://picasaweb.google.com/"

element = browser.text_field(:name, "Email")
element.flash
element.value = mail_address

element = browser.text_field(:name, "Passwd")
element.flash
element.value = password

element = browser.button(:name, "signIn")
element.flash
element.click

IEが起動して、メールアドレス欄、パスワード欄、ログインボタンが点滅しつつログイン後の画面に遷移すると思います。

イメージ

Google Chrome を操作したい場合

  1. chromedriver をダウンロード&解凍して、PATHの通っているところに置く
  2. あとは先程のコードを、以下のように一部だけ変える。
sample_autologin_picasaweb.rb
#require "watir"            # for IE
require "watir-webdriver"   # for Firefox/Chrome

#browser = Watir::IE.new
browser = Watir::Browser.new :chrome

制限

IEは問題無いとして、Chrome とかだと既存のインスタンスにAttach できないみたい。

# まだ試してないけど代わりにこんな感じでいけるらしい
browser.window(:title => 'annoying popup').use do
  browser.button(:id => 'close').click
end 

誰か教えて下さい

以下のような要素を click するにはどう書けばいいのでしょうか?

<div class="goog-inline-block goog-toolbar-button" id=":f" roll="button">

下記のいずれもエラーにならなかったけど、クリックもされなかったのです。なぜ?

browser.div(:class,"goog-inline-block goog-toolbar-button").click
browser.div(:id => ":f").click

参考

19
18
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
19
18