32

iframe を含む Web ページがあります。CasperJSを使用して iframe のコンテンツにアクセスしたいと思います。特に、ボタンをクリックしてフォームに入力する必要があります。どうやってやるの?

メイン Web ページは main.htmlです。

<html><body>
<a id='main-a' href="javascript:console.log('pressed main-a');">main-a</a>
<iframe src="iframe.html"></iframe>
<a id='main-b' href="javascript:console.log('pressed main-b');">main-b</a>
</body></html>

iframe は次のとおりです。

<html><body>
<a id='iframe-c' href="javascript:console.log('pressed iframe-c');">iframe-c</a>
</body></html>

私の素朴なアプローチ:

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

casper.start("http://jim.sh/~jim/tmp/casper/main.html", function() {
    this.click('a#main-a');
    this.click('a#main-b');
    this.click('a#iframe-c');
});

casper.run(function() {
    this.exit();
});

a#iframe-cセレクターがメイン フレームで有効でないため、もちろん機能しません。

[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://jim.sh/~jim/tmp/casper/main.html, HTTP GET
[debug] [phantom] Navigation requested: url=http://jim.sh/~jim/tmp/casper/main.html, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://jim.sh/~jim/tmp/casper/main.html"
[debug] [phantom] Navigation requested: url=http://jim.sh/~jim/tmp/casper/iframe.html, type=Other, lock=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 2/2 http://jim.sh/~jim/tmp/casper/main.html (HTTP 200)
[debug] [phantom] Mouse event 'click' on selector: a#main-a
[info] [remote] pressed main-a
[debug] [phantom] Mouse event 'click' on selector: a#main-b
[info] [remote] pressed main-b
[debug] [phantom] Mouse event 'click' on selector: a#iframe-c
FAIL CasperError: Cannot dispatch click event on nonexistent selector: a#iframe-c
#    type: uncaughtError
#    error: "CasperError: Cannot dispatch click event on nonexistent selector: a#iframe-c"
CasperError: Cannot dispatch click event on nonexistent selector: a#iframe-c    
  /tmp:901 in mouseEvent
  /tmp:365 in click
  /tmp/test.js:9
  /tmp:1103 in runStep
  /tmp:324 in checkStep

これを機能させる方法はありますか?phantomjs を直接突っ込むようなハックならいいのですが、どうすればいいのかわかりません。

CasperJS バージョン 1.0.0-RC1 と phantomjs バージョン 1.6.0 を使用しています。

4

5 に答える 5

41

これを永遠に探していましたが、もちろん、質問を投稿して数分後に答えを見つけました。

この commitで、phantomjs に追加された新しいフレーム切り替えコマンドを使用できます。具体的には、 関数this.page.switchToChildFrame(0)this.page.switchToParentFrame()関数です。文書化されていないようで、今後のリリースでメソッドが変更されているようですが、機能します。

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

casper.start("http://jim.sh/~jim/tmp/casper/main.html", function() {
    this.click('a#main-a');
    this.click('a#main-b');
    this.page.switchToChildFrame(0);
    this.click('a#iframe-c');
    this.page.switchToParentFrame();
});

casper.run(function() {
    this.exit();
});
于 2012-08-27T22:12:22.497 に答える
35

1.0からwithFrameを使用できます

  casper.open("http://www.example.com/page.html", function() {
    casper.withFrame('flashHolder', function() {
      this.test.assertSelectorExists('#the-flash-thing', 'Should show Flash');
    });
  });
于 2013-01-04T09:16:58.647 に答える
4

実際のところ、それらとそのコンテンツにアクセスできるようにするに --web-security=no は、によって提供される新機能を使用する必要があります。Phantomjs 1.5iFrames

于 2012-08-27T22:02:08.363 に答える
3

異なるフレーム (frame1 と frame2) があり、それらのフレームの異なる要素 (クリックや div タグが存在するかどうかの確認など) にアクセスする必要があるとします。

casper.withFrame('frame1', function() {
    var file = '//*[@id="profile_file"]';
    casper.thenClick(x(file));
});

casper.withFrame('frame2', function() {
  casper.then(function () {
     casper.waitForSelector('#pageDIV',
            function pass() {
                console.log("pass");
            },
            function fail(){
                console.log("fail");
            }
      );
   });
});
于 2015-10-28T13:28:49.873 に答える