简体   繁体   English

如何模拟使用Zombie.js按Enter键

[英]How to emulate press Enter key with Zombie.js

Do you know how to press Enter key with Zombie.js ? 你知道如何用Zombie.js按Enter键吗?

Thanks in advance. 提前致谢。

There's no way to do that yet using just zombie API. 使用zombie API还没有办法做到这一点。 It's because .fire() method doesn't allow you to pass any event data in addition to event name (which would be necessary to state which keyCode is associated with that key event). 这是因为除了事件名称之外, .fire()方法不允许您传递任何事件数据(必须说明哪个keyCode与该键事件相关联)。

WTK is correct and there's no native way in zombie.js but I think you could add a javascript function to simulate the enter key press and trigger it from zombie.js like so: WTK是正确的,并且在zombie.js中没有原生方式,但我认为你可以添加一个javascript函数来模拟输入键按下并从zombie.js触发它,如下所示:

If you have access to the page source, add a function on your page to simulate the enter key press: 如果您有权访问页面源,请在页面上添加一个功能以模拟输入键按下:

function pressEnterKey(elmSelector){
  elmSelector = elmSelector || 'document'
  var e = jQuery.Event("keypress");
  e.which = 13; 
  e.keyCode = 13;
  $(elmSelector).trigger(e);
}

Trigger it from zombie.js: 从zombie.js触发它:

browser.evaluate("pressEnterKey()");

If you don't have access to the source you could inject the script on the page using something like this. 如果您无权访问源代码,则可以使用此类内容在页面上注入脚本。 Remember to use browser.wait after to ensure the page is ready: 请记住使用browser.wait以确保页面准备就绪:

var injectedScript = browser.document.createElement("script");
injectedScript.setAttribute("type","text/javascript");
injectedScript.innerText = '...pressEnterKey function text here...'
browser.body.appendChild(injectedScript); 

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

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