@@ -410,31 +410,22 @@ def set_clipboard(self, text, label=None):
410410
411411 def clear_text (self ):
412412 """ clear input text """
413- try :
414- # 这个问题基本不大
415- # 不过考虑到u2.jar不一定升级成功了,所以还有留个兜底方案·
416- self .jsonrpc .clearInputText ()
417- except :
418- self ._clear_text_with_ime ()
413+ self .jsonrpc .clearInputText ()
419414
420- def send_keys (self , text : str , clear : bool = False ):
415+ def send_keys (self , text : str ):
421416 """
422417 send text to focused input area
423418
424419 Args:
425420 text: input text
426421 clear: clear text before input
427422 """
428- if clear :
429- self .clear_text ()
430- try :
431- # setClipboard的兼容性并不是特别好,但是这样做有个优点就是不用装输入法了。
432- self .clipboard = text
433- if self .clipboard != text :
434- raise UiAutomationError ("setClipboard failed" )
435- self .jsonrpc .pasteClipboard ()
436- except :
437- self ._send_keys_with_ime (text )
423+ # 使用el =self(focused=True); el.set_text(el.get_text()+text)不可取
424+ # 因为placeholder中的文字也会加进去
425+ self .clipboard = text
426+ if self .clipboard != text :
427+ raise UiAutomationError ("setClipboard failed" )
428+ self .jsonrpc .pasteClipboard ()
438429
439430 def keyevent (self , v ):
440431 """
@@ -869,7 +860,32 @@ def swipe_ext(self) -> SwipeExt:
869860
870861class Device (_Device , _AppMixIn , _PluginMixIn , InputMethodMixIn , _DeprecatedMixIn ):
871862 """ Device object """
872- pass
863+
864+ def clear_text (self ):
865+ """ clear input text """
866+ if self .is_input_ime_installed ():
867+ InputMethodMixIn .clear_text (self )
868+ else :
869+ _Device .clear_text (self )
870+
871+ def send_keys (self , text : str , clear : bool = False ):
872+ """
873+ send text to focused input area
874+
875+ Args:
876+ text: input text
877+ clear: clear text before input
878+ """
879+ if clear :
880+ self .clear_text ()
881+ if self .is_input_ime_installed ():
882+ InputMethodMixIn .send_keys (self , text )
883+ return
884+ try :
885+ _Device .send_keys (self , text )
886+ except :
887+ # 安装输入法后继续输入
888+ InputMethodMixIn .send_keys (self , text )
873889
874890
875891class Session (Device ):
0 commit comments