我正在尝试测试一个网页,该网页将使用 document.referrer
检测引荐来源网址。我在下面使用这段代码
page = webpage.create();
page.customHeaders = {
"Referer" : "http://example.com"
};
但是它不起作用!! document.refferer
从不检测 referrer,从 phantomjs 访问。
最佳答案
请注意 page.customHeaders
确定虚拟浏览器向服务器发送 的 HTTP header ,而不是浏览器接收 的 HTTP header ,document.referrer
返回后者。
您可能想导航到 http://example.com
首先,然后打开您要访问的页面,最好是放下 <a href="http://localhost/MyApp"/>
标签,然后让幻影点击它。
关于使用的注意事项,您还需要在检查 document
之前确保页面已加载目的。
这是我使用 CasperJS 编写的示例因为它抽象了所有异步和事件处理功能,使代码更具可读性。即:
// Step 1, Navigate to http://example.com and drop a link to your app
casper.start("http://example.com", function() {
this.evaluate(function() {
var link = document.createElement("A");
link.id = "the-button";
link.href= "http://localhost/MyApp";
document.body.appendChild(link);
});
});
// Step 2, pretend to click on link, evaluate referrer when loaded
casper.thenClick("a#the-button", function() {
// The page is now loaded
var referrer = this.evaluate(function() {return document.referrer; }
console.log(referrer)
});
关于javascript - Phantomjs customHeaders referrer 未被 javascript 检测到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24370951/