简体   繁体   English

如何使用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: 我有下面的phantomJS代码来获取HTML代码:

var page = require('webpage').create();
var url = 'http://example.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. 只有当鼠标悬停在由JavaScript控制的特定元素上时才会读取我想要获取的内容,因此上面的代码无效。

I want to know how to simulate the mouse hover on a HTML element using the phantomJS code. 我想知道如何使用phantomJS代码模拟鼠标悬停在HTML元素上。 Let's say I want to mouse hover over on a element then dump the HTML to output, how should I do? 假设我想鼠标悬停在一个元素上,然后将HTML转储到输出,我该怎么办?

Edit: The following code didn't work. 编辑:以下代码不起作用。 What could be the problem? 可能是什么问题呢?

var page = require('webpage').create();
var url = 'http://example.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: 尝试使用jquery 加载页面:

page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    //(...)
});

and then call hover on your element: 然后在你的元素上调用hover:

$('#some_element').trigger('hover');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM