如何在CasperJS中点击下载按钮后获取目标URL?附按钮代码
Got it, let's figure out how to grab that download URL when clicking the button in CasperJS. Since the button uses a JavaScript onclick handler instead of a direct href link, we can't just pull the URL from the attribute—here are two solid approaches to make this work:
When you click the Download button, the page will almost certainly fire a new network request to fetch the file. We can listen for this request in CasperJS and capture its URL directly.
Here's a working code example:
var casper = require('casper').create(); casper.start('your-target-page-url', function() { // Set up a listener for all outgoing resource requests this.on('resource.requested', function(requestData, request) { // Filter requests to find the download link—adjust the condition to match your URL's pattern // For example, look for URLs containing "content", "contents", or a file extension like .pdf/.zip if (requestData.url.includes('content') || requestData.url.endsWith('.pdf')) { console.log('Download URL captured:', requestData.url); // Exit CasperJS once we have the URL casper.exit(); } }); // Simulate clicking the Download button using its onclick attribute as a selector this.click('a[onclick="return Support.DownloadContent(\'\', \'content\',\'contents\');"]'); }); casper.run();
Notes for this approach:
- Adjust the filtering condition in
resource.requestedto match the actual download URL's unique traits (e.g., domain path, file extension). - If the button takes time to become clickable (due to dynamic loading), add a
waitForSelectorstep to ensure it's ready:casper.start('your-target-page-url') .waitForSelector('a[onclick="return Support.DownloadContent(\'\', \'content\',\'contents\');"]', function() { // Run click and network listener code here });
The button triggers Support.DownloadContent('', 'content','contents')—we can run this function directly in the page's context and capture the URL it generates, instead of simulating a click.
This works if the function constructs the download URL and redirects via window.location.href:
var casper = require('casper').create(); casper.start('your-target-page-url', function() { var downloadUrl = this.evaluate(function() { // Override window.location.href to catch the redirect URL without leaving the page var currentUrl = window.location.href; var capturedUrl; Object.defineProperty(window.location, 'href', { set: function(newUrl) { capturedUrl = newUrl; // Prevent actual navigation return currentUrl; }, get: function() { return currentUrl; } }); // Call the same function the button uses Support.DownloadContent('', 'content','contents'); return capturedUrl; }); if (downloadUrl) { console.log('Download URL retrieved:', downloadUrl); } else { console.log('Could not capture URL via direct JS execution—try the network monitoring method instead.'); } }); casper.run();
Notes for this approach:
- If the
Support.DownloadContentfunction returns the URL directly (instead of redirecting), you can simplify theevaluateblock to justreturn Support.DownloadContent('', 'content','contents'); - Some sites use CSRF tokens or session-specific parameters in download URLs—make sure the page has fully loaded all required data before running this code.
内容的提问来源于stack exchange,提问作者timo

