简体   繁体   English

使用CasperJS将鼠标悬停在元素上

[英]Hover over element with CasperJS

How to perform click event on the element which becomes visible after hovering on an element. 如何在元素上执行单击事件,将其悬停在元素上后将变得可见。 below is the HTML code 以下是HTML代码

<div class="jqtree-element jqtree_common">
<span class="jqtree-title jqtree_common" contenteditable="true">Notebook 1</span>
<span class="notebook-right">
<span class="notebook-date disappear" style="visibility: visible;">
<span class="notebook-commands-right">
<span class="notebook-commands appear-wrapper">
<span class="notebook-commands appear" style="display: none;">
<span class="fontawesome-button info">
<span class="fontawesome-button history">
<span class="fontawesome-button private">
<span class="fontawesome-button public" style="display: none;">
<span class="fontawesome-button remove">
<i class="icon-remove"></i>
</span>
</span>
</span>
</span>
</div>

To delete the Notebook i am using following code 要删除我正在使用以下代码的笔记本

casper.then(function(){
  if(this.visible({type:'xpath', path:'/html/body/div[3]/div/div[1]/div[1]/div/div/div[1]/div[2]/div/div/ul/li[1]/ul/li[1]/ul/li[11]/div/span[2]/span[3]/span/span[5]/i'}))
  {
    this.click({type:'xpath', path:'/html/body/div[3]/div/div[1]/div[1]/div/div/div[1]/div[2]/div/div/ul/li[1]/ul/li[1]/ul/li[11]/div/span[2]/span[3]/span/span[5]/i'});
    });
    console.log('notebook '+ title +' deleted');
  } else {
     console.log('element not found');
  }
});

in console it is displaying "Cannot dispatch mousedown event.. " 在控制台中,它显示“无法调度mousedown事件。”

CasperJS has the mouse module which has the move() function. CasperJS的鼠标模块具有move()函数。 It either takes a coordinate or a selector. 它需要一个坐标或一个选择器。 CasperJS will use the underlying PhantomJS sendEvent() function to create a native event. CasperJS将使用基础的PhantomJS sendEvent()函数创建本地事件。

casper.mouse.move(someCSSSelector);

or 要么

var x = require('casper').selectXPath;
...
casper.mouse.move(x(someXPathExpression));

It may be necessary to wait a little after moving the mouse if the page loads something after that: 如果此后页面加载了一些内容,则可能需要在移动鼠标后稍等一下:

casper.mouse.move(someCSSSelector);
casper.waitUntilVisible(expectedElementSelector, function(){
    this.click(expectedElementSelector);
});

Almost all functions work with XPath expressions as well as with CSS selectors. 几乎所有功能都可以与XPath表达式以及CSS选择器一起使用。

If I'm not wrong, I guess what you mean is triggering click event. 如果我没看错,我想你的意思是触发点击事件。 It can be done with native javascript, here's it: 可以使用本机javascript完成,操作如下:

 var note = document.getElementById('note'); note.onclick = function(){ alert('Someone click me'); } var evtClick = document.createEvent('Event'); evtClick.initEvent('click', true, true); note.onmouseover = function() { this.dispatchEvent(evtClick); }; 
 <b id='note'>Hover me!</b> It will trigger click event. 

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

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