- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过 PhantomJS 登录 AliExpress,但我的脚本无法正常工作,而且我不知道为什么。
我使用的是 PhantomJS 版本 2.0.0。
登录表单位于 iframe 内,也许这就是问题所在。
这是代码:
var page = new WebPage(), step = 0, loadInProgress = false, timeOut = false;
var url = 'https://login.aliexpress.com/buyer.htm?spm=2114.11040108.1000002.7.8yVuzJ&return=http%3A%2F%2Fcl.aliexpress.com%2F';
在 i-frame 内填写和提交表单的函数。
var sendForm = function(){
var iframeRef = function( frameRef ) {
return frameRef.contentWindow
? frameRef.contentWindow.document
: frameRef.contentDocument;
};
var iframe = iframeRef(document.getElementById('alibaba-login-box'));
var arr = iframe.getElementById('login-form');
arr.elements["fm-login-id"].value="my-email";
arr.elements["fm-login-password"].value="my-password";
iframe.getElementById('login-form').submit();
console.log("Form submitted.");
};
这些是我的程序遵循的步骤:
var onFinishedSteps = [
//Opens the web to log-in.
function(){
page.open(url);
},
//Fills the form and submits it.
function(){
page.render('0before fill.png');
page.evaluate(sendForm);
page.render('1just after submit.png');
},
//Renders the web ~10 seconds after submitting.
function(){
console.log("timeout setted");
timeOut = true;
window.setTimeout(
function () {
console.log("timeout function");
page.render('2timeout.png');
timeOut = false;
},
10000 // wait 5,000ms (5s)
);
}
];
首先加载网页,然后填写并提交(并呈现)表单。这两个步骤效果很好。渲染的图像正确显示了填充的输入(电子邮件和密码)和 i 框架内容。
第三步应该给程序足够的时间来处理表单和登录,但渲染的图像仍然显示表单,这次密码输入为空并且没有消息(例如“密码不正确”或类似的内容)。
这是程序的其余部分:
page.onNavigationRequested = function(url, type, willNavigate, main) {
console.log('Trying to navigate to: ' + url);
console.log('Caused by: ' + type);
console.log('Will actually navigate: ' + willNavigate);
console.log('Sent from the page\'s main frame: ' + main);
};
page.onLoadStarted = function() {
loadInProgress = true;
};
page.onLoadFinished = function(status) {
loadInProgress = false;
};
interval = setInterval(function() {
if (!loadInProgress && typeof onFinishedSteps[step] == "function") {
console.log("----------------------- Step " + (step + 1));
onFinishedSteps[step]();
step++;
}
if (!loadInProgress && !timeOut && typeof onFinishedSteps[step] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
如果您想测试它,这里有一个包含完整文件的链接。GitHub:https://github.com/xAlstrat/PhantomJsFunctions/blob/master/aliexlogin.js
最佳答案
我认为问题在于您无法在不同框架(iframe)中的元素上调用函数。您可以尝试以下几种方法。
使用 --web-security=false
命令行选项运行 PhantomJS:
phantomjs --web-security=false script.js
在尝试在框架上下文中执行某些操作之前,先更改为框架上下文。您可以使用page.switchToFrame()
或其他类似的函数,通过名称或帧索引更改为帧上下文。例如:
page.switchToFrame(1); // Try this out
page.evaluate(function(){
var arr = document.getElementById('login-form');
arr.elements["fm-login-id"].value="my-email";
arr.elements["fm-login-password"].value="my-password";
document.getElementById('login-form').submit();
console.log("Form submitted.");
});
这会产生一个综合提交事件。
您可以通过使用 page.sendEvent()
触发回车键来触发 native 提交事件。功能。例如,像之前的建议一样:
page.switchToFrame(1); // Try this out
page.evaluate(function(){
var arr = document.getElementById('login-form');
arr.elements["fm-login-id"].value="my-email";
arr.elements["fm-login-password"].value="my-password";
arr.elements["fm-login-password"].focus();
});
page.sendEvent("keypress", page.event.key.Enter);
也许您甚至需要将用户名和密码作为 native 事件“输入”到字段中,因为仅设置字段值不会触发可能在这些元素上注册的任何事件。这可能会触发验证,在提交表单之前可能需要运行验证:
page.switchToFrame(1); // Try this out
page.evaluate(function(){
document.getElementById("fm-login-id").focus();
});
page.sendEvent("keypress", "my-email");
page.evaluate(function(){
document.getElementById("fm-login-password").focus();
});
page.sendEvent("keypress", "my-password");
page.evaluate(function(){
document.getElementById("fm-login-password").focus();
});
page.sendEvent("keypress", page.event.key.Enter); // or page.event.key.Return
关于javascript - AliExpress iframe 中的登录表单在 PhantomJS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298694/
我正在尝试开始使用 Mermaid CLI,但是当我尝试针对我的源文件运行它时,它说找不到 phantomjs。 (我运行的是 Win 7 64。) C:\Users\Chris\Documents>
我正在使用PhantomJS来调用网页,如下所示: page.open('http://example.com', function (s) { console.log(page.content)
如果有这样的功能,我需要更改Phantomjs自动清除缓存的默认时间。有什么想法吗? 最佳答案 应该是您正在寻找的功能: https://github.com/ariya/phantomjs/issu
我想从我的程序运行 PhantomJs 脚本,但由于脚本可能不是我写的,我需要确保 PhantomJs 在执行完成或因任何原因(例如无效语法、超时、 ETC)。到目前为止,我读到的所有内容都说你必须始
在 Package JSON 中,我尝试了许多不同版本的 karma-phantomjs-launcher、phantomjs,包括 phantomjs-prebuilt。 当前包 JSON “开发依
我的脚本有一些语法错误,但 PhantomJS 没有显示任何错误,而是没有显示任何内容。如果脚本有错误,为什么 Phantom JS 不显示解析错误? 在以下 PhantomJS 脚本(通过 Wind
我有一些需要填写的动态输入表单。问题是要填写表格,我需要访问另一个页面以获取取决于上一页输入的数据。因此,在我获得数据然后返回表单后,表单已经更改,因此我需要在获取数据时保持该表单打开。那么问题是如何
PhantomJS 在为我捕获网页到图像文件方面做得很好。我正在使用基于 rasterize.js 的脚本。 但是,对于某些固定大小的 Web 元素,我需要生成的图像与 Web 元素的大小相匹配。 例
我正在将 PhantomJS headless 浏览器集成到我的一个项目中(目前使用 1.6 版)。在大多数情况下,它在完成我需要完成的工作方面做得很好。但是,WebPage.open() 调用工作方
是否可以在page.evaluate中传递变量? function myFunction(webpage, arg1, arg2){ var page = require('webpage').cre
有没有办法始终如一地检测 PhantomJS/CasperJS?我一直在处理用它构建的一系列恶意垃圾邮件机器人,并且能够根据某些行为基本上阻止它们,但是我很好奇是否有一种坚如磐石的方法来了解 Casp
有没有办法拦截资源请求并直接从处理程序给出响应?像这样的事情: page.onRequest(function(request){ request.reply({data: 123}); });
phantomjs 有配置 loadImage, 但我想要更多, 如何控制phantomjs跳过下载某种资源, 比如css等... ===== 好消息:已添加此功能。 https://code.goo
我正在尝试在 PhantomJS (2.1.1) 的 page.evaulate() 调用中使用 WebSocket。当尝试连接到 WebSocket 服务器时,出现以下错误: 安全错误:DOM 异常
我正在使用 PhantomJS 1.8,但遇到了一个限制——您无法指定它用于磁盘缓存的目录。我将其添加到他们的问题跟踪系统中,但由于以前没有它,所以我不希望它很快添加。 因此,我正在寻找解决此限制的方
我想渲染一个仅在用户滚动页面时加载图像的页面。仅设置 page.scrollPosition 没有任何效果。我需要一些可以随时间改变滚动位置的东西。 最佳答案 不确定这是否是最好的方法,但它确实有效。
我正在尝试使用 PhantomJS 设置远程调试,但运气不佳。我按照 https://github.com/ariya/phantomjs/wiki/Troubleshooting 上的说明进行操作。
在 PhantomJS 中,webpage.open 会使用状态参数设置为“成功”或“失败”的回调。根据文档,如果没有发生网络错误,则“成功”,否则“失败”。有没有办法查看导致失败的底层网络错误? 当
有什么方法可以使用 phantomjs 请求资源并能够到达响应的主体吗? 最佳答案 更新:关于“获取并使用所有其他资源(如图像、CSS、字体等)做某事”的其他可能含义,我最近在博客上写了 how to
在运行 PhantomJS 提供的 rasterize.js 示例时,我发现我必须等待 20 秒或更长时间才能生成网页图像。 有没有可能在不消耗大量资源的情况下加快速度的方法?我基本上希望快速生成从加
我是一名优秀的程序员,十分优秀!