如何使用phantomJS来模拟一个HTML元素的鼠标悬停 [英] How to use phantomJS to simulate mouse hover on a HTML element

查看:1685
本文介绍了如何使用phantomJS来模拟一个HTML元素的鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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();
});


解决方案

尝试负载您使用jQuery页面:

  page.includeJs(http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js功能(){
    //(...)
});

然后调用悬停你的元素:

  $('#some_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');

这篇关于如何使用phantomJS来模拟一个HTML元素的鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆