Appium Appium 跑用例,点击播放视频,UIAutomator 无识别弹框上的 ID,求助大神,有什么好方法

逍遥老弟 · 2016年03月14日 · 最后由 郭慧颖 回复于 2018年08月07日 · 2151 次阅读

1、视频自动化,点击半窗,无法通过 id/video_title 获取播放视频名称,用 HierarchyViewer 可以识别出 ID 为 id/video_title

2、代码:

System.out.println(driver.findElementById("com.huawei.hwvplayer.youku:id/video_title").getText());

3、图片:

4、错误信息:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 222 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.huawei.hwvplayer.youku, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=X2P5T14B17000047, platform=LINUX, appActivity=com.huawei.hwvplayer.framework.MainActivity, desired={appPackage=com.huawei.hwvplayer.youku, appActivity=com.huawei.hwvplayer.framework.MainActivity, newCommandTimeout=200, platformVersion=6.0, unicodeKeyboard=true, platformName=Android, deviceName=Android Test, resetKeyboard=true}, newCommandTimeout=200, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, unicodeKeyboard=true, platformName=Android, resetKeyboard=true}]
Session ID: 7311fbc5-60b3-468e-8cf5-3db606330bae
*** Element info: {Using=id, value=com.huawei.hwvplayer.youku:id/video_title}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:358)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:399)
    at com.test.HwVPlayer.HwVPlayerAutoTestCase_0012.HwVPlayerAutoTestCase_0012(HwVPlayerAutoTestCase_0012.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
共收到 29 条回复 时间 点赞

这个按钮的 Node Detail 的截图发出来看看呢

是这个不?


你都说用 uiautomator 了,为啥不用 uiautomatorviewer 查看你这个 test view 的 Node Detail?

你试试用 video_title 作为 id 的值

#3 楼 @hcc3352779
用 uiautomatorviewer,识别不出来的 video_title

#4 楼 @seveniruby 试过,都是找不到控件的

是不是操作快?你延迟看看

#6 楼 @zsd 你打印下 pageSource 确认下那个控件是不是存在吧

#5 楼 @zsd 我发现你的这个 TextView 里面的东西就没被 uiautomatorviewer 识别出来,你肯定没法用 ID 找到对应控件

#9 楼 @hcc3352779 嗯,uiautomatorviewer 只能识别出来一个框,里面的暂停,标题控件什么都识别不了

#7 楼 @wut0n9 不是,延迟了,也没用的

你这是什么 APP,我下载个看看

#8 楼 @seveniruby
用这个是可以确认控件存在,因为这控件点屏幕才会出现,过几秒又会消息,可每次获取的时候都会报错的

#12 楼 @wut0n9 随便一个视频都是这样的,就是你半窗播放时,点击半窗后,会出现暂停什么的按钮控件的

#13 楼 @zsd 获取的时候就已经不存在了. 所以报错. 我建议你用其他工具做辅助. 比如 uiautomator 的 watch 机制. 或者自己加入重试机制.

#13 楼 @zsd 那你就先模拟下点击动作再定位该元素呀

#16 楼 @wut0n9 点击也是报找不到元素, An element could not be located on the page using the given search parameters

Appium 是根据 uiautomatorviewer 框架来定位和识别元素的,uiautomatorviewer 识别不出来,你别想用 webdriver 的对应方法找到元素

#18 楼 @hcc3352779 大神能不能从其它方面来解决呢

#19 楼 @zsd 你用 driver.contexts 试下,看看除了 native app,还有其它东西没.因为你的目的是获取那段文字而不是做类似点击的动作,所以必须要定位到对应元素才行

#20 楼 @hcc3352779 我后面还是要执行点击操作的

#21 楼 @zsd 如果只是单纯点击,你找到这个元素的 X,Y 坐标,然后用 driver.tap() 就行

#22 楼 @hcc3352779 driver.contexts 还是报错的

io.appium.java_client.NoSuchContextException: No such context found. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 63 milliseconds
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'

#23 楼 @zsd Python 里的写法是 driver.contexts, Java 也是这样?

#25 楼 @zsd
我查了下 Appium Java Client 的方法, 你这方法没用对啊

你先找到暂停键控件,点到暂停后再获取 title 的 text 属性。我是直接用的暂停键坐标。

while True:
    try:
        os.system("adb shell input tap 100 1000")
        time.sleep(1)
        os.system("adb shell input tap 100 1000")

        title = WebDriverWait(driver,6).until(lambda x:x.find_element_by_id("com.huawei.hwvplayer.youku:id/video_title"))
        text = title.get_attribute("name")
        print (text)
        display = True
    except:
        print ("none")
        display = False

    if display == True:

        break
    else:
        continue

请问楼主的问题解决了吗,我现在也是遇到这个问题,播放器中的很多元素基本无法定位,有的能找到有的找不到;
能不能实现获取播放器中的播放时长和进度条的变化(判断视频或音频是否正常开始播放)

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册