最新消息:20210917 已从crifan.com换到crifan.org

【规避解决】uiautomator2对于安卓浏览器的输入框set_text输入文字无效

安卓 crifan 1201浏览 0评论
折腾:
【未解决】用uiautomator2操作安卓浏览器打开百度首页
期间,代码:
BaiduHomeUrl = "https://www.baidu.com/“
inputUiObj.set_text(BaiduHomeUrl)
不生效:没有给地址栏中输入百度首页网址。
以及:
searchStr = “crifan"
inputUiObj.set_text(searchStr)
也没输入希望的文字。
且发现了页面上:
输入框上层并没有可以点击的 搜索框元素
去看看
openatx/uiautomator2: Android Uiautomator2 Python Wrapper (github.com)
找到有些 貌似相关的:
These key names are currently supported:

* search
* enter


Input method
这种方法通常用于不知道控件的情况下的输入。第一步需要切换输入法,然后发送adb广播命令,具体使用方法如下
d.set_fastinput_ime(True) # 切换成FastInputIME输入法
d.send_keys("你好123abcEFG") # adb广播输入
d.clear_text() # 清除输入框所有内容(Require android-uiautomator.apk version >= 1.0.7)
d.set_fastinput_ime(False) # 切换成正常的输入法
d.send_action("search") # 模拟输入法的搜索
send_action 说明
该函数可以使用的参数有 go search send next done previous
什么时候该使用这个函数呢?
有些时候在EditText中输入完内容之后,调用press("search") or press("enter")发现并没有什么反应。 这个时候就需要send_action函数了,这里用到了只有输入法才能用的IME_ACTION_CODE。 send_action先broadcast命令发送给输入法操作IME_ACTION_CODE,由输入法完成后续跟EditText的通信。(原理我不太清楚,有了解的,提issue告诉我)
自己多试试再说,看看如何触发搜索
inputUiObj.set_text(searchStr)
并没有输入内容
只是触发了输入法
不过:
d.press("enter")
倒是可以触发:回车键,搜索了。
手动点击掉:允许
其中可见是:默认的文字,搜索的结果
此处确认的是:触发了输入法弹框,看来是可以输入内容的。
继续试试
参考自己之前的:
def selectorSetText(self, curXpathSelector, inputText):
    # Special: add click to try workaround for 360 pwd EditText input but input to 360 account EditText
    # curXpathSelector.click()
    # curXpathSelector.clear_text()
    selectorSetTextResp = curXpathSelector.set_text(inputText)
    logging.debug("selectorSetTextResp=%s", selectorSetTextResp) # selectorSetTextResp=None
    # 在set_text后,输入法会变成FastInputIME输入法
    # 用下面代码可以实现:关掉FastInputIME输入法,切换回系统默认输入法
    self.driver.set_fastinput_ime(False)
你还是去换用xpath的selector,去试试
此处是:
等了几秒,可以输入了:
但是:
一是耗时
二是,输入法被切换了
所以要加上:
d.set_fastinput_ime(False)
切换回原先输入法
直接运行后,结果:
却不起效果:没有输入百度网址
加上断点运行,此处可以恢复输入法了:
想起来了:
此处,未必算是uiautomator2的bug或者不兼容问题
而是:
本身此处浏览器顶部输入框,用户手指点击了,都是进入了输入法弹框的界面,再输入内容的。
所以,此处,代码上应该:对于元素去click,进入输入和搜索界面才对
去写代码
inputUiObj = d(resourceId="com.android.browser:id/b4h", className="android.widget.TextView")
inputUiObj.click()
即可进入希望的:输入法弹框输入界面:
【总结】
此处,其实不是uiautomator的UiObject或XPathSelector的set_text不生效问题
而是:本身页面元素,就是点击了,应该进去新的界面(输入法弹框界面,用于输入内容)
所以应该把代码:
inputUiObj = d(resourceId="com.android.browser:id/b4h", className="android.widget.TextView")
inputUiObj.set_text(BaiduHomeUrl)
换成:
inputUiObj = d(resourceId="com.android.browser:id/b4h", className="android.widget.TextView")
inputUiObj.click()
或:
inputXpathSelector = d.xpath("//android.widget.TextView[@resource-id='com.android.browser:id/b4h']”)
inputXpathSelector.click()
去触发新界面,即可。
-》后续再去定位搜索框,输入内容(百度首页地址),进入百度首页

转载请注明:在路上 » 【规避解决】uiautomator2对于安卓浏览器的输入框set_text输入文字无效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.178 seconds, using 23.31MB memory