Simulate TAB keypress event in Selenium RC

为君一笑 提交于 2019-12-03 15:41:39

问题


I need to simulate a tab keypress in Selenium RC, using the Java API.

I do this after having entered some text using:

selenium.type(input, "mytext");

I've tried 3 alternatives to get the tab working:

selenium.keyPress(input, "\\9");

and:

selenium.focus(input);
selenium.keyPressNative("09");

and even:

selenium.getEval("var evt = window.document.createEvent('KeyboardEvent');evt.initKeyEvent ('keypress', true, true, window,0, 0, 0, 0,0, 9,0);window.document.getElementsByTagName('input')[2].dispatchEvent(evt);")

The best I can get is a "tab space" to be inserted after my text so I end up with this in the input field:

"mytext    "

What I actually want is to tab to the next control. Any clues? Thanks!

(Note: I have to use tab and can not use focus or select to chose the element I want to go to, for various reasons, so no suggestions along these lines please!)


回答1:


selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + ""); 

I don't use the Java API, but this post from google groups suggests it is your solution. I can't imagine that "9" is different from "09" in your question, but give it a try?




回答2:


Try the official TAB char: \t or \u0009




回答3:


Some functions may used Onblur. It will trigger the function when the field lose the key focus. here we can use fireEvent with "blur" or "focus" command as follows: command: fireEvent target: id=your_field_identification value: blur

Reference: http://qaselenium.blogspot.com/2011/01/how-to-triger-tab-key-in-selenium.html




回答4:


Improvising Ryley's answer, we can use

selenium.keyDownNative(java.awt.event.KeyEvent.VK_TAB + "");
selenium.keyUpNative(java.awt.event.KeyEvent.VK_TAB + "");

I tried this method for VK_CONTROL in IE and it worked good.




回答5:


Use typeKeys():

Quoting the above link:

Unlike the simple "type" command, which forces the specified value into the page directly, this command may or may not have any visible effect, even in cases where typing keys would normally have a visible effect. For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in the field.

In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to send the keystroke events corresponding to what you just typed.



来源:https://stackoverflow.com/questions/3140616/simulate-tab-keypress-event-in-selenium-rc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!