webdriver.io使用enter的方式
【问题】使用webdriver.io 需要实现一个UI自动化输入input 并 回车
pageObeject
import {keyboard} from "../utils/keyboard-mouse-operation"; class CommonPage { get searchInput() { return $('input[name="search"]') } get saveButton() { return $('.portal-button--primary=SAVE') } get deleteButton() { return $('.portal-button--primary=DELETE') } get doneButton() { return $('.portal-button--secondary=DONE') } searchKeyword(keyword){ this.searchInput.click() //this.searchInput.setValue(keyword) this.searchInput.setValue(`${keyword}\n`) // keyboard.press('backspace',80) //browser.keys(['Enter']) // browser.keys("\uE007") browser.pause(5000) } } export default new CommonPage()
【尝试方法1】
自定义工具类进行输入enter,并调用
keyboard.press('backspace',80)
定义工具类 类名为 keyboard-mouse-operation
module.exports.keyboard ={ key: { 'enter': '\ue007', 'backspace': '\u0008', 'esc':'\u001B' }, press(button, num=1){ if (button in this.key){ const actions = [ { "type": "key", "id": "keyboard", "actions": [ {"type": "keyDown", "value": this.key[button]}, {"type": "keyUp", "value": this.key[button]}, ] } ] let i = 0 while(i < num){ browser.performActions(actions) i++ } }else{ console.log("The button isn't defined for the method") } } } module.exports.mouse = { key: { 'left': 0 }, click(button, x=0, y=0, num=1){ if (button in this.key){ const actions = [ { "type": "pointer", "id": "mouse", "parameters": {"pointerType": "mouse"}, "actions": [ {"type": "pointerMove", "duration": 0, "origin": "pointer", "x": x, "y": y}, {"type": "pointerDown", "button": this.key[button]}, {"type": "pointerUp", "button": this.key[button]} ] } ] let i = 0 while(i < num){ browser.performActions(actions) i++ } }else{ console.log("The button isn't defined for the method") } } }
【尝试方法2】
使用框架自带的方法
browser.keys("\uE007");
对应代码可看selenium.dev/selenium/docs/api/rb/Selenium/WebDriver/Keys.html
KEYS = See Also: Element#send_keys http://www.google.com.au/search?&q=unicode+pua&btnK=Search { null: "\ue000", cancel: "\ue001", help: "\ue002", backspace: "\ue003", tab: "\ue004", clear: "\ue005", return: "\ue006", enter: "\ue007", shift: "\ue008", left_shift: "\ue008", control: "\ue009", left_control: "\ue009", alt: "\ue00A", left_alt: "\ue00A", pause: "\ue00B", escape: "\ue00C", space: "\ue00D", page_up: "\ue00E", page_down: "\ue00F", end: "\ue010", home: "\ue011", left: "\ue012", arrow_left: "\ue012", up: "\ue013", arrow_up: "\ue013", right: "\ue014", arrow_right: "\ue014", down: "\ue015", arrow_down: "\ue015", insert: "\ue016", delete: "\ue017", semicolon: "\ue018", equals: "\ue019", numpad0: "\ue01A", numpad1: "\ue01B", numpad2: "\ue01C", numpad3: "\ue01D", numpad4: "\ue01E", numpad5: "\ue01F", numpad6: "\ue020", numpad7: "\ue021", numpad8: "\ue022", numpad9: "\ue023", multiply: "\ue024", add: "\ue025", separator: "\ue026", subtract: "\ue027", decimal: "\ue028", divide: "\ue029", f1: "\ue031", f2: "\ue032", f3: "\ue033", f4: "\ue034", f5: "\ue035", f6: "\ue036", f7: "\ue037", f8: "\ue038", f9: "\ue039", f10: "\ue03A", f11: "\ue03B", f12: "\ue03C", meta: "\ue03D", command: "\ue03D", # alias left_meta: "\ue03D", # alias zenkaku_hankaku: "\uE040", right_shift: "\ue050", right_control: "\ue051", right_alt: "\ue052", right_meta: "\ue053", numpad_page_up: "\ue054", numpad_page_down: "\ue055", numpad_end: "\ue056", numpad_home: "\ue057", numpad_left: "\ue058", numpad_up: "\ue059", numpad_right: "\ue05A", numpad_down: "\ue05B", numpad_insert: "\ue05C", numpad_delete: "\ue05D" }.freeze
【尝试方法3】
直接在传参的后面加入回车
element.setValue(`${value}\n`);
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-10-11 一些项目总结
2018-10-11 【路由】路由下一跳的作用