单击按钮在 CasperJS 中不会执行任何操作
Clicking a button doesn't do anything in CasperJS
我正在尝试自动化以下站点: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();
我得到的结果
图片1:
图片二:
看图片。您可以看到,我的代码适用于 2 个输入(邮政编码和位置类型)。它点击显示弹出窗口。现在我的问题出现了。我不知道如何让它点击弹出窗口中的一个框(这个框有几天可以发货)
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'
表示第一次点击打开,第二次点击关闭的模态
我正在尝试自动化以下站点: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();
我得到的结果
图片1:
图片二:
看图片。您可以看到,我的代码适用于 2 个输入(邮政编码和位置类型)。它点击显示弹出窗口。现在我的问题出现了。我不知道如何让它点击弹出窗口中的一个框(这个框有几天可以发货)
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'
表示第一次点击打开,第二次点击关闭的模态