Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
casperjs/phantomjs如何单击href=";javascript:function";并获取结果内容_Javascript_Web Scraping_Phantomjs_Casperjs_Php Phantomjs - Fatal编程技术网

casperjs/phantomjs如何单击href=";javascript:function";并获取结果内容

casperjs/phantomjs如何单击href=";javascript:function";并获取结果内容,javascript,web-scraping,phantomjs,casperjs,php-phantomjs,Javascript,Web Scraping,Phantomjs,Casperjs,Php Phantomjs,我是phantomjs/casperjs的新手。 我试图单击一个具有javascript函数的href(href='javascript:getPhoneNumber(..),我想得到一个png图像的打印结果。 以下是单击之前的html块: <div class="logo_text_link"> <img class="logo_text_link" src="//static.awebsite.com/img/devices/mobile/awebsite_mob

我是phantomjs/casperjs的新手。 我试图单击一个具有javascript函数的href(href='javascript:getPhoneNumber(..),我想得到一个png图像的打印结果。 以下是单击之前的html块:

<div class="logo_text_link">

    <img class="logo_text_link" src="//static.awebsite.com/img/devices/mobile/awebsite_mobile_view_phone.png" alt="Telephoner">

    <span id="phoneNumber"><a class="nohistory adview_links" href='javascript:getPhoneNumber("https://api.awebsite.com", 1117004764, "54bb0281238b45a03f0ee695f73e704f")'>See&nbsp;the&nbsp;number&nbsp;</a></span
</div>
<div class="logo_text_link">

    <img class="logo_text_link" src="//static.awebsite.com/img/devices/mobile/awebsite_mobile_view_phone.png" alt="Telephoner">

  <span id="phoneNumber"><div class="phoneimg"></div><img class="AdPhonenum" src="https://www.awebsite.com/pg/0f/AxLHLu0zHVAtWTp+nJCc2KkcZTdPfX3CM=.gif"></span>

</div>

如果有人知道如何完成这项任务,我会非常感激(我已经挣扎了两天了)

我已经把我的答案写进了一个带有注释的长代码示例中;使用真实网站更好地解释要点。请确保打开该站点并检查脚本的作用

var img; // We only really need one variable to get that image address

// Let's create a CasperJS instance that tries (although not fanatically)
// to mimic a browser: it has a real resolution instead of default 400x300 
// and a Chrome useragent (better to use those to not confuse websites into thinking 
// this is a mobile browser with small screen; they could even redirect to a mobile version)
// Of course if you _want_ a mobile version use resolution and useragent of a mob.browser
var casper = require('casper').create({
    pageSettings: {
        userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
    },
    viewportSize : { width: 1280, height: 1024 },
    verbose: true
});
var x = require('casper').selectXPath;

casper.start("http://www.autreys.com/categories/firearms.html");

casper.then(function() {
    // make screenshots often to check that CasperJS sees what you think it sees
    casper.capture("1-before-click.jpg");    

    // Click on the first button of this type to open a lightbox
    casper.click(x('(//*[@class="QuickViewBtn"])[1]'));
});

console.log("This will be output before everything else because it is not put in the queue like casper.then or casper.wait");

// Give the lightbox some time to load (3 seconds)
casper.wait(3000);

// After that let's get the cover image from the lightbox
casper.then(function() {
    casper.capture("2-after-click.jpg");    
    img = casper.evaluate(function() {
        // This happens "in the browser"
        // You cannot assign local variables like "img" here
        // Just return variables "back"
        return document.querySelector("#QuickViewImage img").src;
    });
    console.log(img);
});

casper.run();

最好在你的问题中提供一个真实的URL。
var img; // We only really need one variable to get that image address

// Let's create a CasperJS instance that tries (although not fanatically)
// to mimic a browser: it has a real resolution instead of default 400x300 
// and a Chrome useragent (better to use those to not confuse websites into thinking 
// this is a mobile browser with small screen; they could even redirect to a mobile version)
// Of course if you _want_ a mobile version use resolution and useragent of a mob.browser
var casper = require('casper').create({
    pageSettings: {
        userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
    },
    viewportSize : { width: 1280, height: 1024 },
    verbose: true
});
var x = require('casper').selectXPath;

casper.start("http://www.autreys.com/categories/firearms.html");

casper.then(function() {
    // make screenshots often to check that CasperJS sees what you think it sees
    casper.capture("1-before-click.jpg");    

    // Click on the first button of this type to open a lightbox
    casper.click(x('(//*[@class="QuickViewBtn"])[1]'));
});

console.log("This will be output before everything else because it is not put in the queue like casper.then or casper.wait");

// Give the lightbox some time to load (3 seconds)
casper.wait(3000);

// After that let's get the cover image from the lightbox
casper.then(function() {
    casper.capture("2-after-click.jpg");    
    img = casper.evaluate(function() {
        // This happens "in the browser"
        // You cannot assign local variables like "img" here
        // Just return variables "back"
        return document.querySelector("#QuickViewImage img").src;
    });
    console.log(img);
});

casper.run();