【问题标题】:How do i select value from dropdown of a hybrid app using appium?如何从使用 appium 的混合应用程序的下拉列表中选择值?
【发布时间】:2018-09-20 08:43:18
【问题描述】:

我对 appium 比较陌生,并且积极参与 poc。要求是从下拉列表中选择一个值,但是我可以单击微调器元素,但下面的任何内容都无法识别我的 uiautomator。结果,我无法从微调器元素中选择任何值。 我附上了脚本的代码块以及元素树快照。

 //trying to click the dropdown list
		 try{
			 WebElement parentElement1 = driver.findElement(By.id("retProdOp0"));
			 WebElement childElement1 = parentElement1.findElement(By
					.xpath("//android.view.View[@index='1']"));
			childElement1.click();
			driver.label("dropdown list 2nd element clicked");
		 }catch(Exception e){
			driver.label("Failed to click dropdown list on prodexchg screen");
			System.out.println(e.getMessage());
		 }
		 

Snapshot1

Snapshot2

我想从 Snapshot2 中微调器的下拉列表中选择值。但是我无法在 uiautomator 中找到它们。寻求帮助。提前致谢。

【问题讨论】:

    标签: spinner appium android-uiautomator


    【解决方案1】:

    您使用了不正确的 XPath 定位器来选择下拉列表中的项目。

    在您的 uiautomator 屏幕截图中,很明显,在您单击下拉菜单之前,其中的项目尚未在 DOM 中。所以在 parent 中搜索是不正确的,因为它当时不包含元素。

    1. 使用appium-desktop 代替 UIAutomator 进行元素检查,效果更好
    2. 点击下拉菜单将其展开
    3. 要单击其中一项,请执行以下操作:

    List<WebElement> items = driver.findElements(By.xpath("//android.view.View")); items.get(0).click();

    driver.findElement(By.xpath("(//android.view.View)[1])

    【讨论】:

    • 嗨,dmle,尝试了你所说的,但在 appium-desktop 中也找不到元素。
    【解决方案2】:

    尝试了 appium-desktop,但遇到了同样的问题。 Appium-desktop screenshot 1 Appium-desktop screenshot 2

    请参考第二张截图。谢谢

    【讨论】:

      【解决方案3】:

      如果我理解正确,您的应用程序的 WebView 在 HTML/JS 中显示下拉菜单。

      我以https://www.w3schools.com/howto/howto_js_dropdown.asp 为例加载并使用CulebraTester 单击按钮然后单击“Link 3”项生成测试。

      生成的脚本是

      #! /usr/bin/env python
      # -*- coding: utf-8 -*-
      '''
      Copyright (C) 2013-2018  Diego Torres Milano
      Created on 2018-04-11 by CulebraTester 
                            __    __    __    __
                           /  \  /  \  /  \  /  \ 
      ____________________/  __\/  __\/  __\/  __\_____________________________
      ___________________/  /__/  /__/  /__/  /________________________________
                         | / \   / \   / \   / \   \___
                         |/   \_/   \_/   \_/   \    o \ 
                                                 \_____/--<
      @author: Diego Torres Milano
      @author: Jennifer E. Swofford (ascii art snake)
      '''
      
      
      import re
      import sys
      import os
      
      
      import unittest
      try:
          sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
      except:
          pass
      
      import pkg_resources
      pkg_resources.require('androidviewclient>=12.4.0')
      from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
      from com.dtmilano.android.uiautomator.uiautomatorhelper import UiAutomatorHelper, UiScrollable, UiObject, UiObject2
      
      TAG = 'CULEBRA'
      
      
      class CulebraTests(CulebraTestCase):
      
          @classmethod
          def setUpClass(cls):
              cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
              cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': True, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
              cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 1, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': None, 'unit-test-method': None, 'interactive': False}
              cls.sleep = 5
      
          def setUp(self):
              super(CulebraTests, self).setUp()
      
          def tearDown(self):
              super(CulebraTests, self).tearDown()
      
          def preconditions(self):
              if not super(CulebraTests, self).preconditions():
                  return False
              return True
      
          def testSomething(self):
              if not self.preconditions():
                  self.fail('Preconditions failed')
      
              _s = CulebraTests.sleep
              _v = CulebraTests.verbose
      
              UiScrollable(self.vc.uiAutomatorHelper, uiSelector='clazz@android.webkit.WebView,index@0,parentIndex@0,package@com.android.chrome').getChildByText(uiSelector='text@Click Me', text="Click Me", allowScrollSearch=True).click()
              UiScrollable(self.vc.uiAutomatorHelper, uiSelector='clazz@android.webkit.WebView,index@0,parentIndex@0,package@com.android.chrome').getChildByText(uiSelector='text@Link 3', text="Link 3", allowScrollSearch=True).click()
      
      
      if __name__ == '__main__':
          CulebraTests.main()
      

      这似乎工作正常。

      如果使用appium找不到其他解决方案,可以尝试一下。

      【讨论】:

        【解决方案4】:

        以下解决方案将为您提供帮助。如果没有,请告诉我。

        1. 从您的对象属性看来,您现在处于本机上下文中。因此,在选择下拉项之前,请先更改为 Web Context。

        driver.Context = "WebContext";

        driver.Context = "CHROMIUM";

        您的网络上下文的名称可以不同 2. 现在像在 w​​eb 中使用 selenium webdriver 一样选择元素

        Select dropdown = new Select(driver.findElement(By.id("mySelect"))); dropdown.selectByVisibleText("Text");

        1. 现在,您可以根据需要切换回混合应用程序的本机上下文。

        注意:要查找 webcontext 的属性,您可以获取源代码或转到浏览器中的 Web URL。

        【讨论】:

          猜你喜欢
          • 2017-06-09
          • 2020-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-03
          • 2022-07-25
          • 1970-01-01
          • 2015-12-20
          相关资源
          最近更新 更多