LoginSignup
8

More than 5 years have passed since last update.

RustからphantomJSを叩く

Posted at

前置き

PhantomJS は、バージョン1.8から、GhostDriver が取り込まれ、直接 JsonWireProtocolを叩く事が出来るようになりました。

このプロトコルはHTTPベースなので、hyperで叩くだけで簡単に扱えます。

今回はそのアプローチで、RustからPhantomJSを叩いてみようと思います。

PhantomJSをwebdriver有りで起動する

PhantomJSが、JsonWireProtocolを喋れるようにするには下記コマンドを実行します。

phantomjs --ignore-ssl-errors=yes --webdriver=8910

ignore-ssl-errors は、SSL証明書が検証できない場合において発生するエラーを無視するフラグですが、これは自由に設定して頂いて構いません。

--webdriver=8910 で、phantomjsはサーバーとして起動し、ポート 8910 で待機します。

webdriverについて補足

Selenium Webdriver というテストツールのことで、ブラウザテストを簡単に行えるツールです。
このツールではJsonWireProtocol を話すことが出来る各Driver(Firefox Driver等さまざま)を経由し、テストを行います。しかし、FirefoxやChrome等は直接JsonWireProtocolを喋ることができないので、それぞれのDriverがありますが PhantomJSは内部にDriverを持っている(これがGhostDriver)ので、直接喋る事ができます。

--webdriverというパラメータなので、ややこしいですね。

PhantomJSを叩く

今回も サンプルコード を用意しました。

extern crate ghostdriver_client;
extern crate cookie;
use cookie::Cookie as CookiePair;
use std::collections::BTreeMap;

fn main() {

    let mut phantomjs_session = ghostdriver_client::get_session("localhost".to_owned(),
                                                                "8910".to_owned(),
                                                                "Mozilla/5.0 (Macintosh; Intel \
                                                                 Mac OS X 10_11_0) \
                                                                 AppleWebKit/537.36 (KHTML, like \
                                                                 Gecko) Chrome/46.0.2490.80 \
                                                                 Safari/537.36"
                                                                    .to_owned());
    phantomjs_session.jump_to_url("http://google.com".to_owned());
    phantomjs_session.execute_script("document.body.style.backgroundColor = 'red'; return '1';"
                                         .to_owned());
    phantomjs_session.capture_screenshot("foo.png".to_owned());
    phantomjs_session.set_cookies(vec![CookiePair {
                                           name: "foo".to_owned(),
                                           value: "bar".to_owned(),
                                           expires: None,
                                           max_age: None,
                                           domain: None,
                                           path: None,
                                           secure: false,
                                           httponly: false,
                                           custom: BTreeMap::new(),
                                       }]);
    println!("{:?}", phantomjs_session.get_cookies());

}

Crate内部でやっていることは、hyperでHTTPを叩いているだけです。
まだ出来ることは限られていますが、メソッドを増やすことで他の機能も利用出来るようになります。

例えば、 JsonWireProtocol
session/:sessionId/element/:id/click をラップしたメソッドを作ることで、DOM要素をクリックさせることも可能です。

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
What you can do with signing up
8