javascript - How to open different popups with the same title in CasperJS? -
i'm trying automate tasks using casperjs, , need open multiple popups. however, popups have exact same url (http://.../printit.aspx/...), whenever use
this.withpopup(/printit/, function() {...});
it opens first popup. can't access other ones.
i suppose there 2 possibilities :
- close each popup after visiting it, can't find how this
- accessing popups using way url regex /printit/. maybe using
casper.popups
, documentation vague this.
there no easy , documented way of disambiguating 2 popups. documentation says casper.popups
array-like property. iterate on it. judging by code, popups
property pagestack
. 1 can modify pagestack.findbyregexp()
function kind of thing.
it seems casper.popups
property contains duplicate entries, 1 can filter them out.
casper.findallpopupsbyregexp = function(regexp){ var popups = this.popups.filter(function(popuppage) { return regexp.test(popuppage.url); }); if (!popups) { throw new caspererror(f("couldn't find popup url matching pattern %s", regexp)); } // remove duplicates var uniquepopups = []; popups.foreach(function(p){ if (uniquepopups.indexof(p) === -1) { uniquepopups.push(p); } }); return uniquepopups; }
casper.withpopup()
accepts 3 types of inputs identify popup page. third 1 page object itself. can retrieve matching popup page objects findallpopupsbyregexp()
, select 1 want , pass withpopup()
change context:
casper.then(function(){ var popups = this.findallpopupsbyregexp(/printit/); this.withpopup(popups[1], function(){ ... }); });