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

【已解决】用uiautomator2给安卓浏览器中百度首页中输入文字并触发搜索

搜索 crifan 486浏览 0评论
折腾:
【未解决】Mac中用uiautomator2自动操作安卓手机浏览器实现百度搜索
期间,现已代码操作打开浏览器,进入了百度首页了
接着去,输入内容,触发百度搜索,显示搜索结果页面。
Prop    Value
activity    .BrowserActivity
package    com.android.browser
className    android.widget.EditText
description    
text    
resourceId    index-kw
clickable    true
label    
x, y    44, 628
width    723
height    140
enabled    true
visible    
XPath    //*[@resource-id="index-kw"]
position     (0.176, 0.291) 点击
代码 d(resourceId="index-kw")
和:
Prop    Value
activity    .BrowserActivity
package    com.android.browser
className    android.widget.Button
description    
text    百度一下
resourceId    index-bn
clickable    true
label    
x, y    764, 628
width    275
height    140
enabled    true
visible    
XPath    //*[@resource-id="index-bn"]
position     (0.826, 0.299) 点击
代码 d(resourceId="index-bn")
去写代码
searchStr = "crifan"

baiduSearchInputUiObj = d(resourceId="index-kw", className="android.widget.EditText")
baiduSearchInputUiObj.set_text(searchStr)
效果:
然后先去试试 模拟输入回车键 触发百度搜索:
################################################################################
# Trigger baidu search
################################################################################


# Method 1: press Enter
EnterKey = "enter"
d.press(EnterKey)
print("Emulated press key %s" % EnterKey)
结果:
是可以正常触发百度搜索的。
另外,注意到前面输入法中,右下角是 搜索 按钮,所以换去试试:
# Method 1: press key
# TriggerSearchKey = "enter"
TriggerSearchKey = "search"
d.press(TriggerSearchKey)
print("Emulated press key %s" % TriggerSearchKey)
模拟输出 search,结果:
竟然没反应。说明此处无效。
再去试试其他几个官网
openatx/uiautomator2: Android Uiautomator2 Python Wrapper (github.com)
提到的
go search send next done previous
好像不对。应该是:
  • home
  • back
  • search
  • enter
没有其他选项了。不过也去试了:
# TriggerSearchKey = "enter" # work
# TriggerSearchKey = "search" # not work
# TriggerSearchKey = "go" # not work
TriggerSearchKey = "done" # not work
d.press(TriggerSearchKey)
其他没用
-》此处就只是enter有效,能触发搜索。
继续试试,换用:
找到百度一下按钮,点击触发百度搜索:
Prop    Value
activity    .BrowserActivity
package    com.android.browser
className    android.widget.Button
description    
text    百度一下
resourceId    index-bn
clickable    true
label    
x, y    770, 248
width    266
height    127
enabled    true
visible    
XPath    //*[@resource-id="index-bn"]
position     (0.907, 0.145) 点击
代码 d(resourceId="index-bn")
用代码:
# Method 2: find 百度一下 button then click
baiduSearchButtonUiObj = d(resourceId="index-bn", className="android.widget.Button")
baiduSearchButtonUiObj.click()
print("Clicked baidu search button")
结果:
同样可以 触发百度搜索。
【总结】
此处最后用代码:
################################################################################
# Input text
################################################################################
searchStr = "crifan"


baiduSearchKeywordUiObj = d(resourceId="index-kw", className="android.widget.EditText")
baiduSearchKeywordUiObj.set_text(searchStr)
print("Inputed baidu search text %s" % searchStr)


################################################################################
# Trigger baidu search
################################################################################


# # Method 1: press key
# TriggerSearchKey = "enter" # work
# # TriggerSearchKey = "search" # not work
# # TriggerSearchKey = "go" # not work
# # TriggerSearchKey = "done" # not work
# d.press(TriggerSearchKey)
# print("Emulated press key %s" % TriggerSearchKey)


# Method 2: find 百度一下 button then click
baiduSearchButtonUiObj = d(resourceId="index-bn", className="android.widget.Button")
baiduSearchButtonUiObj.click()
print("Clicked baidu search button")
可以实现:
给百度首页中
输入要搜索的文字
并(模拟按下回车键,或,找到百度一下按钮并点击)触发搜索
此处的效果是:

转载请注明:在路上 » 【已解决】用uiautomator2给安卓浏览器中百度首页中输入文字并触发搜索

发表我的评论
取消评论

表情

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

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