【问题标题】:Clicking a button doesn't do anything in CasperJS在 CasperJS 中单击按钮不会执行任何操作
【发布时间】:2016-06-05 16:09:35
【问题描述】:

我正在尝试自动化以下站点:http://www.1800flowers.com/orchids/purple-mini-101006?categoryId=400065010

我的代码是:

var casper = require("casper").create();
var mouse = require("mouse").create(casper);

var casper = require('casper').create({
    verbose: true,
    //logLevel: 'debug',     // debug, info, warning, error
    clientScripts: ["includes/jquery.min.js"],
    pageSettings:{
        loadImages: false,
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'
    }
}
);

var fs = require('fs');

casper.start().thenOpen("http://www.1800flowers.com/orchids/purple-mini-101006?categoryId=400065010", function(){
    console.log("Access Site 1800.Com");
    this.wait(10000);
});

casper.then(function(){
    this.capture("1800_1.png");
});

casper.then(function(){
    this.evaluate(function(){
        $('#zipCode').attr('value', '12345');
        $('#locationCode').attr('value', 'Residence');

        //document.querySelector('select[id="locationCode"]').focus();
    });
    this.mouse.click('#calendarLink');
    this.wait(20000);
    this.mouse.click('#DeliveryCalendarGPTDayCharges');
    this.wait(20000);
    //this.sendEvent('keypress', this.page.event.key.Down);
});

casper.then(function(){
    this.capture("1800_2.png");
    console.log(this.getCurrentUrl());
});

casper.run();

我得到的结果

图一:

图二:

看图片。您可以看到,我的代码使用 2 个输入(邮政编码和位置类型)。它点击显示弹出窗口。现在我的问题出现了。我不知道要让它点击弹出窗口中的一个框(这个框有几天可以发货)

【问题讨论】:

    标签: javascript click casperjs


    【解决方案1】:

    casper.mouse.click 不是异步步进函数,但casper.wait 是异步的。这意味着它是按这个顺序执行的:

    this.evaluate(function(){ ... });
    this.mouse.click('#calendarLink');
    this.mouse.click('#DeliveryCalendarGPTDayCharges');
    this.wait(20000);
    this.wait(20000);
    

    动态页面的时间可能不足以让第二次点击产生任何效果。您需要将这些点击放在单独的步骤中:

    this.evaluate(function(){ ... });
    this.mouse.click('#calendarLink');
    this.wait(20000, function(){
        this.mouse.click('#DeliveryCalendarGPTDayCharges');
    });
    this.wait(20000);
    

    或更短:

    this.evaluate(function(){ ... });
    this.thenClick('#calendarLink')
        .wait(20000)
        .thenClick('#DeliveryCalendarGPTDayCharges')
        .wait(20000);
    

    这是一个更强大的示例,其中包含适当的 wait* 函数:

    this.thenClick('#calendarLink')
        .waitUntilVisible('#TB_window', function(){
            this.capture("1800_popup.png");
        }, null, 60000)
        .thenClick('#DeliveryCalendarGPTDayCharges')
        .waitWhileVisible('#TB_window', null, null, 60000);
    

    这里'#TB_window'表示第一次点击后打开,第二次点击后关闭的modal。

    【讨论】:

    • 它不适用于我。如果我在 Firefox 的控制台上尝试 $('#DeliveryCalendarGPTDayCharges').click(); => 它工作 100%。但是在 casperjs 上 this.mouse.click("#DeliveryCalendarGPTDayCharges");或 $('#DeliveryCalendarGPTDayCharges').click(); => 当我拍摄时没有任何东西,图片仍然显示弹出日历。你能访问我的网站并进行测试吗?请帮我。我努力了很长时间 3 - 4 天,但我没有任何东西 :(
    • 它适用于我的 CasperJS 1.1-beta5 和 PhantomJS 2.1.1。
    • 哦,谢谢!现在它对我有用,抱歉我的网络有问题,所以需要更长的时间。
    • 你能帮我写一段代码等待弹出显示(#DeliveryCalendarGPTDayCharges)并等待(#calendarLink)吗?我是新手,我需要真实的例子来学习。谢谢!
    • 我添加了一个更强大的示例。如果这解决了您的问题,您可以accept 回答。请检查您之前的问题,看看您是否可以接受其中任何一个答案。
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    相关资源
    最近更新 更多