大家好,目前在做应用商店的自动化脚本,想做的是,在下载管理界面,监控当前正在下载的进度,如果正常下载就 print 下载进度,如果出现异常,则 print 错误,脚本片段如下

from appium import webdriver as wd
#省略通用格式的代码
wbdriver = wd.Remote('http://127.0.0.1:4723/wd/hub',dc)
#紧跟着的脚本是点击一个下载,然后点击下载管理界面的按钮,跳转到下载管理的activity

downnumbutton = wbdriver.find_element_by_id('downPointsText') #显示 暂停、继续、重试三种下载状态的按钮
loading_text = wbdriver.find_element_by_id('loading_text') #进度条文字

while wbdriver.current_activity == '.mdcontent.usermanager.down.UserDownActivity':
    if downnumbutton.text == '暂停':
        print('正在下载,进度: ',loading_text.text,'\r',end='')
    if downnumbutton.text == '继续' or downnumbutton.text == '重试':
        print('下载出错')
        wbdriver.quit()

运行以上脚本,每次 python 都是会报错,信息如下:

File "d:\haimamarket\downandinstall.py", line 37, in
print('正在下载,进度: ',loading_text.text,'\r',end='')
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 69, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 456, in _execute
return self._parent.execute(command, params)
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
raise wde
File "C:\Python35\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

查看 Appium 中的信息是:

info: [debug] Getting focused package and activity
info: [debug] executing cmd: D:\Android\sdk\platform-tools\adb.exe -s 3DN4C16411024563 shell "dumpsys window windows"
info: [debug] Responding to client with success: {"status":0,"value":".mdcontent.usermanager.down.UserDownActivity","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: <-- GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/appium/device/current_activity 200 75.297 ms - 118 {"status":0,"value":".mdcontent.usermanager.down.UserDownActivity","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: --> GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/4/text {}
info: [debug] Pushing command to appium work queue: ["element:getText",{"elementId":"4"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getText","params":{"elementId":"4"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getText
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"鏆傚仠"}
info: [debug] Responding to client with success: {"status":0,"value":"鏆傚仠","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: <-- GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/4/text 200 49.005 ms - 80 {"status":0,"value":"鏆傚仠","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: --> GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/5/text {}
info: [debug] Pushing command to appium work queue: ["element:getText",{"elementId":"5"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getText","params":{"elementId":"5"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getText
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"UiSelector[INSTANCE=0, RESOURCE_ID=me.haima.androidassist:id\/loading_text]"}
info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"UiSelector[INSTANCE=0, RESOURCE_ID=me.haima.androidassist:id/loading_text]"},"sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}

我个人分析原因是:
当下载完成后,商店会自动调用安装,因此当前的 activity 就不再是 .mdcontent.usermanager.down.UserDownActivity ,理论上 current_activity 方法应该能检测到 activity 变化,但是,在商店调起安装的时候,current_activity 返回的结果还是 .mdcontent.usermanager.down.UserDownActivity 。所以,依然进入了 while 循环,此时报错,找不到
控件

请教各位大神,我分析的原因是不是对的呢?这种情况下,我应该如何做,才能实现我的目的: 监控下载进度,当下载完成调起安装的 activity 后,能操作安装的 activity 谢谢


↙↙↙阅读原文可查看相关链接,并与作者交流