简体   繁体   English

单击一个按钮,直到它在CasperJS中消失

[英]Click on a button until it disappears in CasperJS

I have this sample of my code: 我有我的代码示例:

function clickOldShares() {
  console.log("Waiting for all shares");
  element = document.querySelector("#pagelet_scrolling_pager > div > div > a"); 
  return element;
 }      

  casper.thenOpen("https://www.facebook.com/shares/view?id=" + fb_objectID,function(){
    console.log("Open post with object-id");
  });

  casper.then(function(){
    element = this.evaluate(clickOldShares);
  });


  casper.wait(2000,function() {
    console.log('ELEMENT1: ' + element);
    element = this.evaluate(clickOldShares);

  });

  casper.wait(2000,function() {
    newelement = this.evaluate(clickOldShares);  
    console.log('ELEMENT2: ' + newelement);
  });

  casper.wait(2000,function() {
    newelement = this.evaluate(clickOldShares);  
    console.log('ELEMENT3: ' + newelement);

  });

I´m not understanding how can I transform this calls to clickOldShares in a loop using CasperJS because casper.wait is asynchronous. 我不明白如何使用CasperJS在循环中将此调用转换为clickOldShares ,因为casper.wait是异步的。 May I have some example of how to do this, please? 我可以举一些如何做到这一点的例子吗?

The page doesn't load all data in one time. 该页面不会一次加载所有数据。 It's necessary to click on the 'Older Shares' button until the data appears. 有必要单击“旧股”按钮,直到数据出现。 And this can happen many times, depending the amount of data. 这可能会发生很多次,具体取决于数据量。 So, I need to click as often as needed before capturing data. 因此,我需要在捕获数据之前根据需要经常单击。

First thing's first, you can't use clickOldShares for anything as it is now. 首先,你不能像现在这样使用clickOldShares casper.evaluate() provides access to the DOM, but the passed in function is sandboxed and executed in the page context. casper.evaluate()提供对DOM的访问,但传入的函数是沙箱并在页面上下文中执行。 All data must be explicitly passed in and out, and this has to be primitive. 必须明确地传入和传出所有数据,这必须是原始的。 DOM elements are not primitive and cannot be passed out of the page context ( this.evaluate(clickOldShares) will always return null ). DOM元素不是原始元素,不能传递出页面上下文( this.evaluate(clickOldShares)将始终返回null )。 You will either have to call the click code inside of the page context. 您将需要调用页面上下文中的单击代码。


You can wait for an element to appear with waitForSelector . 您可以等待一个元素与waitForSelector一起出现。 You really don't need to iterate to wait for it. 你真的不需要迭代等待它。

var selector = "#pagelet_scrolling_pager > div > div > a";
casper.start()
    .thenOpen(url)
    .waitForSelector(selector, null, null, 15000); // max 15 seconds
    .then(function(){
        this.capture("screen1.png");
        this.click(selector);
    })
    .then(function(){
        this.capture("screen2.png");
    })
    .run();

The third argument for waitForSelector is the callback for when the timeout is reached, but the element is not found. waitForSelector的第三个参数是达到超时的回调,但找不到该元素。 The fourth argument is a custom timeout. 第四个参数是自定义超时。 The default timeout is set to 10 seconds. 默认超时设置为10秒。


It seems you need to click on a certain selector until it disappears. 看来你需要点击某个选择器直到它消失。 You can't use a loop for this, because the functions are asynchronous. 你不能使用循环,因为这些函数是异步的。 You will have to use recursion like this: 你将不得不使用这样的递归:

var selector = "#pagelet_scrolling_pager > div > div > a";
var i = 0;
function step() {
    if (this.exists(selector)) {
        this.capture("screen"+(i++)+".png");
        this.click(selector);
        this.wait(2000, step);
    } else {
        this.capture("screen_final.png");
    }
}

casper.start()
    .thenOpen(url)
    .then(step)
    .then(function(){
        // TODO: do something else
    })
    .run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM