在PhantomJS/CasperJS中设置屏幕大小 [英] Set Screen size in PhantomJS/CasperJS

查看:127
本文介绍了在PhantomJS/CasperJS中设置屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自动访问网站以从中收集数据.不幸的是,该页面检测到屏幕尺寸,如果屏幕太小,则无法提供所需的页面.从非交互式上下文(IIS Web应用程序)运行脚本时,Screen对象似乎返回1024x768像素的大小.即使我将视口大小设置为1300x1000像素,这也会影响屏幕截图,但不会影响网页的JavaScript.

I'm automating access to a web site to collect data from it. Unfortunately that page detects the screen size and doesn't give me the desired page if the screen is too small. When running the script from a non-interactive context (IIS web application) the Screen object seems to return a size of 1024x768 pixels. Even though I set the viewport size to 1300x1000 pixels which affects screenshots but not the web page's JavaScript.

如何告诉PhantomJS或CasperJS(我正在使用后者,但正在使用前者)在页面的JavaScript中为Screen对象返回不同的值?

How can I tell PhantomJS or CasperJS (I'm using the latter but that's using the former) to return different values for the Screen object in page's JavaScript?

这是CasperJS,不是PhantomJS,因此不是重复的

This is CasperJS, not PhantomJS, so it's not a duplicate.

推荐答案

这是CasperJS的工作方式:

Here's how it works with CasperJS:

// at the start of the script file
var casper = require("casper").create({
    // other options here
    onPageInitialized: function (page) {
        page.evaluate(function () {
            window.screen = {
                width: 1920,
                height: 1080
            };
        });
    }
});

// script goes on here

这篇关于在PhantomJS/CasperJS中设置屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆