casperjs-------跳转窗口

突然发现,原来casperjs不支持跳转窗口,为什么呢?我不知道,明明phantomjs是支持的呀!

不过还是有解决的,因为我们可以改呀,获得了DOM后,就不关服务器的事了,要怎么在我们的浏览器上显示,可以由我们自己决定!也就是说,点击或者搜索后会不会在新的窗口页出现,完全由我们自己。

实验网站是笔趣看:”http://www.biqukan.com/“,点击搜索,他会跳转到新的窗口。
那么,我们等他加载好DOM后,用js脚本不允许它跳转。

function rm_new_window() {
    form_ele = document.querySelectorAll("form[action='/s.php']")[0];
    form_ele.removeAttribute("target");
}
# 我们直接移除那个跳转的属性就可以了!

然后我们在casperjs中运行这个js代码!

var casper = require("casper").create({
    pageSettings:{
        loadImages: false
    }
});

function rm_new_window() {
    form_ele = document.querySelectorAll("form[action='/s.php']")[0];
    form_ele.removeAttribute("target");
}

casper.start("http://www.biqukan.com/", function() {
    this.evaluate(rm_new_window);
    # 这里我们执行js代码
    this.fill("form[action='/s.php']", {
        "q": "元尊"
    }, false);
    this.mouseEvent("click", "body > div.header > div > div.search > form > input.btn");
});

casper.then(function() {
    this.waitForSelector("body > div.nav > ul > li:nth-child(3) > a", function() {
        this.capture("D:/图片/元尊.jpg");
        # 你会发现确实截取到了
        this.exit();
    });
});

猜你喜欢

转载自blog.csdn.net/killeri/article/details/80781070