在 Selenium RC 中模拟 TAB 按键事件

发布于 2024-09-07 15:22:20 字数 755 浏览 3 评论 0原文

我需要使用 Java API 在 Selenium RC 中模拟 Tab 键按下。

我在输入一些文本后执行此操作:

selenium.type(input, "mytext");

我尝试了 3 种替代方法来使选项卡正常工作:

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

和:

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

甚至:

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);")

我能得到的最好的结果是在我的文本后面插入一个“制表符空格”,所以我最终得到了这个在输入字段中:

"mytext    "

我真正想要的是切换到下一个控件。有什么线索吗?谢谢!

(注意:由于各种原因,我必须使用选项卡,不能使用焦点或选择来选择我想要转到的元素,所以请不要提出此类建议!)

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!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洒一地阳光 2024-09-14 15:22:21

即兴Ryley的答案,我们可以使用

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

我在IE中尝试过这种方法用于VK_CONTROL,效果很好。

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.

陪你到最终 2024-09-14 15:22:21

使用 typeKeys()

引用上面的链接:

与简单的“type”命令不同,该命令直接将指定的值强制输入页面,此命令可能有也可能没有任何可见效果,即使在通常情况下键入按键会产生可见效果的情况下也是如此。例如,如果您在表单元素上使用“typeKeys”,您可能会也可能不会看到您在字段中键入内容的结果。

在某些情况下,您可能需要使用简单的“type”命令来设置字段的值,然后使用“typeKeys”命令发送与您刚刚键入的内容相对应的击键事件。

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.

千と千尋 2024-09-14 15:22:20
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB + ""); 

我不使用 Java API,但这篇文章建议这是你的解决方案。我无法想象你的问题中的“9”与“09”不同,但是尝试一下?

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?

烟雨扶苏 2024-09-14 15:22:20

尝试官方 TAB 字符:\t\u0009

Try the official TAB char: \t or \u0009

不如归去 2024-09-14 15:22:20

某些功能可能会使用 Onblur。当字段失去焦点时会触发该功能。在这里我们可以将 fireEvent 与“blur”或“focus”命令一起使用,如下所示:
命令:fireEvent
目标:id=your_field_identification
值:模糊

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文