无法获取要点击的链接并使用PhantomJS切换到下一页 [英] Cannot get link to be clicked and switch to next page using PhantomJS
问题描述
我在让phantomJS点击网站上的登录按钮时遇到问题。
I am having an issue getting phantomJS to click the login button on a website.
我可以在我的第二个屏幕截图中看到它正在尝试选择登录按钮,但我无法等待并在下一页上截取屏幕截图。
I can see in my second screenshot that it is trying to select the login button, but I cannot get it to wait and take the screenshot on the next page.
这是我的JS文件:
var page = require('webpage').create();
page.viewportSize = {width: 1920,height: 1080};
page.open('http://clubs.bluesombrero.com/default.aspx?portalid=1809', function (status) {
console.log("Status: " + status);
if (status === "success") {
var url = page.url;
console.log('URL: ' + url);
console.log("TC0001: Pass");
page.render('TC0001.png');
var a = page.evaluate(function() {
return document.querySelector('#dnn_dnnLOGIN_cmdLogin');
});
page.sendEvent('click', a.offsetLeft, a.offsetTop);
page.render('TC0002.png');
} else {
console.log("TC0001: Failed, Page did not load.");
}
phantom.exit();
});
我已尝试了一些方法让它等到页面加载后截取屏幕截图,但我没有运气。
I have tried a few ways to get it to wait to take the screenshot after the page has loaded, but I have not had any luck.
推荐答案
page.sendEvent()
是一个完成的同步函数它的行动一旦完成。即使在点击触发的请求被应答之前,也会执行下一个调用( page.render()
)。
page.sendEvent()
is a synchronous function that finishes as soon as its action is done. The next call (page.render()
) is executed even before the request which was triggered by the click is answered.
JavaScript提供了两个等待静态时间的函数: setTimeout
和 setInterval
:
JavaScript provides two functions to wait a static amount of time: setTimeout
and setInterval
:
page.sendEvent('click', a.offsetLeft, a.offsetTop);
setTimeout(function(){
page.render('TC0002.png');
phantom.exit();
}, 5000);
(别忘了删除其他 phantom.exit()
因为你不想太早退出)
(don't forget to remove the other phantom.exit()
since you don't want to exit too early)
当然现在的问题是,一方面页面仍然可能没有准备就绪5秒或另一方面,页面加载非常快,只是坐在那里什么都不做。
Of course the problem is now that on one hand the page still might not be ready after 5 seconds or on the other hand the page was loaded extremely fast and just sits there doing nothing.
更好的方法是使用 waitFor()
function 在examples文件夹中提供PhantomJS。您可以等待特定元素的特定条件:
A better approach would be to use the waitFor()
function that is provided in the examples folder of PhantomJS. You can wait for a specific condition of the page like the existence of a specific element:
page.sendEvent('click', a.offsetLeft, a.offsetTop);
waitFor(function _testFx(){
return page.evaluate(function(){
return !!document.querySelector("#someID");
});
}, function _done(){
page.render('TC0002.png');
phantom.exit();
}, 10000);
3。 page.onLoadFinished
另一种方法是收听 page.onLoadFinished
将在下一页加载时调用的事件,但您应该在点击之前注册:
3. page.onLoadFinished
Another approach would be to listen to the page.onLoadFinished
event which will be called when the next page is loaded, but you should register to it before you click:
page.onLoadFinished = function(){
page.render('TC0002.png');
phantom.exit();
};
page.sendEvent('click', a.offsetLeft, a.offsetTop);
4。 page.onPageCreated
每当在桌面浏览器中打开新窗口/标签时, page.onPageCreated
将在PhantomJS中触发。它提供了对新创建页面的引用,因为前一页面没有被覆盖。
4. page.onPageCreated
Whenever a new window/tab would be opened in a desktop browser, the page.onPageCreated
would be triggered in PhantomJS. It provides a reference to the newly created page, because the previous page is not overwritten.
page.onPageCreated = function(newPage){
newPage.render('TC0002.png');
newPage.close();
phantom.exit();
};
page.sendEvent('click', a.offsetLeft, a.offsetTop);
在所有其他情况下,页面
实例被新页面覆盖。
In all the other cases, the page
instance is overwritten by the new page.
这可能仍然不够,因为PhantomJS没有指定加载页面时的含义以及页面的JavaScript仍然可能进一步要求建立页面。这个Q& A提供了一些等待完整页面加载的好建议: phantomjs不等待完整页面加载
That might still not be sufficient, because PhantomJS doesn't specify what it means when a page is loaded and the JavaScript of the page may still make further requests to build up the page. This Q&A has some good suggestions to wait for a "full" page load: phantomjs not waiting for "full" page load
这篇关于无法获取要点击的链接并使用PhantomJS切换到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!