如何使用phantomJS来模拟一个HTML元素的鼠标悬停
(How to use phantomJS to simulate mouse hover on a HTML element)
I have the phantomJS code below to fetch the HTML code:
var page = require('webpage').create(); var url = 'http://www.abc.com/'; page.open(url, function (status) { var js = page.evaluate(function () { return document; }); console.log(js.all[0].outerHTML); phantom.exit(); });
The content I want to fetch will only be read while the mouse is hover on the specific element which is controlled by JavaScript, so the code above is not working.
I want to know how to simulate the mouse hover on a HTML element using the phantomJS code. Let's say I want to mouse hover over on a element then dump the HTML to output, how should I do?
Edit: The following code didn't work. What could be the problem?
var page = require('webpage').create(); var url = 'http://www.abc.com/'; page.open(url, function (status) { page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function(){ $('#some_element').trigger('hover'); }); var js = page.evaluate(function () { return document; }); console.log(js.all[0].outerHTML); phantom.exit(); });
Try load your page with jquery:
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { //(...) });
and then call hover on your element:
$('#some_element').trigger('hover');
本文地址:IT屋 » How to use phantomJS to simulate mouse hover on a HTML element
我有以下phantomJS code,以获取HTML code:
VAR页=要求('网页')创建()。
VAR URL ='http://www.abc.com/'; page.open(URL,功能(状态){
VAR JS = page.evaluate(函数(){
返回文件;
});
的console.log(js.all [0] .outerHTML);
phantom.exit();
});
我想取将只读取当鼠标悬停是由JavaScript控制的特定元素上的内容,所以上面的code是行不通的。
我想知道如何模拟使用phantomJS code一个HTML元素的鼠标悬停。比方说,我想鼠标悬停在某个元素上,然后倾倒HTML输出,我应该怎么办?
编辑:下面code没有工作。可能是什么问题?
VAR页=要求('网页')创建()。
VAR URL ='http://www.abc.com/';page.open(URL,功能(状态){
page.includeJs(http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js”功能(){
$('#some_element')触发('悬停')。
});
VAR JS = page.evaluate(函数(){
返回文件;
});
的console.log(js.all [0] .outerHTML);
phantom.exit();
});
page.includeJs(http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js”功能(){
//(...)
});
$('#some_element')触发('悬停');
本文地址:IT屋 » 如何使用phantomJS来模拟一个HTML元素的鼠标悬停