【问题标题】:Capture Popup page content through CasperJS or PhantomJS通过 CasperJS 或 PhantomJS 捕获弹出页面内容
【发布时间】:2025-12-16 02:00:01
【问题描述】:

A.html

<input type="submit" name="testLabel" value="Print Test" id="testLabel" onclick="myFunction('<?php echo $dynamic_page;?>')" >

<script>
function myFunction(page) {
    var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
    window.open(page, "_blank",strWindowFeatures);
}
</script>

CasperJS 代码

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

casper.start('http://localhost/test/a.html', function() {
    this.echo(this.getTitle());
});

casper.thenClick('#testLabel');

casper.then(function() {
    this.capture('page.png');
});

casper.run();

我也尝试过使用 phantomjs,但我无法在 page.png 中捕获 b.html 页面

注意:弹出页面名称不固定。

【问题讨论】:

  • 但在真正的问题弹出屏幕是空白弹出在加载内容时需要时间如何解决这个问题

标签: phantomjs casperjs


【解决方案1】:

// 这会将弹出 DOM 设置为主要活动的 DOM,仅在时间

//正在执行的步骤关闭

casper.withPopup(0, function() {
    this.test.assertTitle('Popup title');
});

完整代码

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

casper.start('http://localhost/test/a.html', function () {
    this.echo(this.getTitle());
});

casper.thenClick('#testLabel');

casper.waitForPopup(0, function () {
    this.echo('Popup');
});

casper.then(function () {
    this.capture('page.png');
});

casper.run();

阅读更多关于waitForPopup

【讨论】:

    【解决方案2】:

    waitForPopupwithPopup 方法,所以你的代码看起来像这样

    var casper = require('casper').create();
    
    casper.start('http://localhost/test/a.html', function() {
        this.echo(this.getTitle());
    });
    
    casper.thenClick('#testLabel');
    
    casper.waitForPopup(/.\.html$/, function() {
        this.echo('Popup')
    });
    
    casper.withPopup(0, function() {
        this.capture('page.png');
    });
    
    casper.run();
    

    【讨论】:

    • 这不起作用,因为我们无法在 waitForPopup 中写入直接值 b.html 这个相关的亚马逊卖家中心抓取
    • 所以不要写直接值b.html。如果您可以看到这是一个正则表达式,那么您可以动态捕获任何您想要的值。
    • 不正确的答案你能用正则表达式完成吗?我在谷歌上搜索然后问,因为你的答案在 stack-overflow 上已经准备好了
    • 这个答案完全回答了你的问题。您可以提供真实示例和真实代码,也可以修改我的代码以使其适合您。说它不正确是错误的
    • 如果您对 amazon cellercentral 有所了解,这与打印发票和标签有关,当您点击打印时,打开 pdf 中的内容,我不知道谁支持您