2016-06-16 2 views
2

저는 9 개의 팝업 링크가있는 Page A를 가지고 있으며 각 팝업에서 정보를 추출해야합니다. 이 후에 나는 페이지 A로 돌아가서 계속 작업해야합니다.CasperJS - 페이지 위로 팝업 링크를 반복하고 모든 팝업을 긁은 후 페이지 A로 돌아갑니다.

for 루프에서 waitForPopup 및 withPopup을 사용했는데 각 팝업을 열고 닫는 올바른 방법이라고 생각합니다. 그러나 이상한 것은 내가 9 번 반복 된 첫 번째 팝업에서 정보를 얻었습니다. 나는 그것을 얻기 위해 밤새도록 노력했지만 붙어있었습니다. 아래 내 코드를 살펴보십시오. 감사.

업데이트 정확한 연결 상태를 발견했습니다.

1> The code is part of the var suite_1 = function() { ...my code... } 

2> The regex /fulltext/ here is based on the popup links 
(only the Gid value varies), like url=http://www.***.com/fulltext_form.aspx&Gid=788... 

3> I also have some debug info. 

[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9] 
[debug] [phantom] Mouse event 'mouseup' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9] 
[debug] [phantom] Mouse event 'click' on selector: xpath selector: (//a[@class="main-ljwenzi"])[9] 
[debug] [phantom] Navigation requested: url=http://www.***.com/fulltext_form.aspx&Gid=788, type=LinkClicked, willNavigate=true, isMainFrame=false 
: 내 스크립트 내부 Casperjs - Is ThenClick Or Any 'then Function' Synchronized Function?

, while 루프가, 내 스크립트가 당신의 내용은 thenClick

전에 While 루프의 끝으로 이동합니다 발견

... 그리고 ...

[info] [phantom] Step anonymous 119/122 http://www.***.com/fulltext_form.aspx&Gid=252923 (HTTP 200) 

The program is expected to open the link with Gid=788, but after some _step, it still open the Gid=252923, which is the first popup. 

HTML (popup.html) : 10 코드

this.then(function() { 

    var count = this.getElementsInfo('a.main').length; 
    this.echo(count + ' fulltext links found:', 'INFO'); 

    for (var i = 1; i < count + 1; i++) { 

    //According to the output, 
the program will run the this.capture 9 times, 
before run the following lines of code 9 times 

     this.capture('before the click - ' + i + '.png'); 

     this.thenClick(x('(//a[@class="main"])[' + i + ']')); 

     this.waitForPopup(/fulltext/, function() { 
      this.echo('Popup opened', 'INFO'); 
      this.echo(this.getTitle(), 'INFO'); 
     }, null, 10000); 

     this.withPopup(/fulltext/, function() { 

      this.echo(this.getTitle(), 'INFO'); 
      this.waitForSelector('#tbl_content_main', function() { 
       // do something 
      }); 
     }); 

     this.then(function() { 
      this.echo('Back to the main page' + this.getTitle(), 'INFO_BAR'); 
     }); 
    } 
}); 

답변

0

내가 당신의 케이스를 테스트하기 위해 간단한 예제를 만들어

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<div> 
    <button class="popup" id="casperjs" onclick="javascript:window.open('http://casperjs.org/')">casperjs</button> 
    <button class="popup" id="phantomjs" onclick="javascript:window.open('http://phantomjs.org/')">phantomjs</button> 
    <button class="popup" id="nodejs" onclick="javascript:window.open('https://nodejs.org/')">nodejs</button> 
</div> 
</body> 
</html> 

코드 :

var casper = require('casper').create(); 

casper.start('http://localhost:63342/popup.html') 

casper.then(function() { 
    var count = casper.getElementsInfo('.popup').length 
    var counter = 1 
    for (var i = 0; i < count; i++) { 
     casper.then(function() { 
      var selector = 'body > div > button:nth-child(' + counter + ')'; 
      var text = casper.getElementInfo(selector).text 
      casper.click(selector) 
      casper.waitForPopup(text, undefined, undefined, 20000) 
      casper.withPopup(text, function() { 
       casper.echo(casper.getTitle()) 
      }) 
      counter ++ 
     }) 
    } 
}) 

casper.run() 

출력 :

$ casperjs popup.js 
CasperJS, a navigation scripting and testing utility for PhantomJS and SlimerJS 
PhantomJS | PhantomJS 
Node.js 

예상대로 작동합니다.

  1. 이를위한 루프 A에 다음 내부의 전체 코드를 포장해야합니다

    그래서, 당신의 코드는이 문제가있다.
  2. i 대신 다른 카운터를 사용하여 선택자를 지정해야합니다 (i의 값을 표시하여 항상 count임을 알 수 있습니다.) 제 경우에는 3입니다.

thenClick과 아무 관련이 없습니다.

이 시도 ... 당신은 내 예제 코드에 thenClickclick를 대체 할 수있는, 모든 것이 당신이 thenClick의 구현을 살펴 경우, 당신이 내 문 뒤에 이유를 찾을 수 ... 동일하게 유지 :

를 다른 카운터를 사용해야하는 이유에 대해 궁금해하는 경우
this.then(function() { 

    var count = this.getElementsInfo('a.main').length; 
    this.echo(count + ' fulltext links found:', 'INFO'); 
    var counter = 1 // + 
    for (var i = 1; i < count + 1; i++) { 
     this.then(function() { //+ 
      this.capture('before the click - ' + counter + '.png');//edit 

      this.thenClick(x('(//a[@class="main"])[' + counter + ']'));//edit 

      this.waitForPopup(/fulltext/, function() { 
       this.echo('Popup opened', 'INFO'); 
       this.echo(this.getTitle(), 'INFO'); 
      }, null, 10000); 

      this.withPopup(/fulltext/, function() { 

       this.echo(this.getTitle(), 'INFO'); 
       this.waitForSelector('#tbl_content_main', function() { 
        // do something 
       }); 
      }); 

      this.then(function() { 
       this.echo('Back to the main page' + this.getTitle(), 'INFO_BAR'); 
      }); 
      counter ++ //+ 
     })//+ 
    } 
}); 

, 다른 SO post에 대한 내 대답을 읽을 수 있습니다.

+0

안녕하세요 Sayakiss, 그때 함수에서 코드를 래핑하려고했지만 실패했습니다. Xpath selector : (// a [@ class = "main-ljwenzi"]) [10]', 왜냐하면 단지 9 개의 링크가 있기 때문입니다. for 루프가 단순히 9 번 실행 된 다음 코드의 클릭 부분을 실행 한 것처럼 보였습니다. 내 코드는 약 200 행입니다. 코드를 전자 메일로 보낼 수 있습니까? – user5404480

+0

@ user5404480 전체 스크립트를 업로드 하시겠습니까? 요점 같은 것이 좋을 것입니다 ... – Sayakiss

+0

1. 팝업에 집중하고 다른 웹 사이트에 간단한 스크립트를 작성할 수 있습니다 (그러나 문제를 재현 할 수 있음). 2. 게으른 사람이라면, 내 이메일 : zhushuierjirou @ gmail.com에 코드를 보낼 수 있습니다. – Sayakiss