javascript 如何在 Chrome devtool 控制台上触发点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18823530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger a tap event on Chrome devtool console?
提问by user2763948
How can I use the Chrome-devtool's console to test if my javascript works? I've located the xpath and converted it to an css locator. Basically it is a button that turns the color from grey to blye.
如何使用 Chrome-devtool 的控制台来测试我的 javascript 是否有效?我找到了 xpath 并将其转换为 css 定位器。基本上它是一个将颜色从灰色变为 blee 的按钮。
Here is my snippet code: browser.execute_script("$('button.nominate').trigger('tap');")
这是我的片段代码: browser.execute_script("$('button.nominate').trigger('tap');")
On the console, I tried something like:
在控制台上,我尝试了类似的操作:
$('button.nominate').trigger('tap')
$('button.nominate').trigger('tap')
The result shown below:
结果如下图所示:
[]
[]
I thought it would tap the button
我以为它会点击按钮
回答by Konrad Dzwinel
I suppose you are doing some kind of functional testing on your mobile app. I was doing the same thing some time ago (using CasperJS) and, in the process, I've created this function:
我想您正在对您的移动应用程序进行某种功能测试。前段时间我也在做同样的事情(使用 CasperJS),在这个过程中,我创建了这个函数:
// I've commented out CasperJS specific stuff, don't use it if you don't need it
function triggerEventOnPage(selector, eventName, memo) {
//casper.evaluate(function(selector, eventName, memo){
var event;
var element = document.querySelector(selector);
event = document.createEvent("Event");
event.initEvent(eventName, true, true);
event.memo = memo || { };
element.dispatchEvent(event);
//}, selector, eventName, memo);
//wait();
}
You can use it in your tests by calling:
您可以通过调用在测试中使用它:
triggerEventOnPage(".edit-list-button", 'tap');
However, mind that there is no native tap
event. There are only touchstart
, tachmove
, touchend
events and implementation of tap
is done based on those three. Therefore, implementation of tap
event that you are using may differ from one that I was using and the function above may not work for you.
但是,请注意没有本地tap
事件。只有touchstart
, tachmove
,touchend
事件和实现tap
是基于这三个完成的。因此,tap
您正在使用的事件的实现可能与我使用的不同,并且上述功能可能对您不起作用。
EDIT:since you are using jQuery, $('button.nominate').trigger('tap')
should work just fine to. @NULL may be right that your selector is invalid.
编辑:因为您使用的是 jQuery,所以$('button.nominate').trigger('tap')
应该可以正常工作。@NULL 可能是正确的,您的选择器无效。