【问题标题】:How to locate elements with Appium and Python?如何使用 Appium 和 Python 定位元素?
【发布时间】:2015-03-27 02:28:35
【问题描述】:
  • 在 Android 设备上测试一个简单的计算器应用程序。
  • 尝试了多种方法来定位元素,但都失败了。
  • 我可以从 UI Automator Viewer 获得的信息是类名、按钮名、坐标。我只想找到“7”按钮并按下它。

代码:

import unittest, time, os
from appium import webdriver
from time import sleep




class Android_Maaii(unittest.TestCase):
    "Class to run tests against the ATP WTA app"
    def setUp(self):
        "Setup for the test"
        PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.2.2'
        desired_caps['deviceName'] = 'Galaxy S4'
        desired_caps['app'] = PATH('C:\Python34\MyCalculator.apk')        
        # Since the app is already installed launching it using package and activity name
        desired_caps['appPackage'] = 'com.example.mythirdapp'
        desired_caps['appActivity'] = '.MainActivity'
        # Adding appWait Activity since the activity name changes as the focus shifts to the ATP WTA app's first page
        desired_caps['appWaitActivity'] = 'com.example.mythirdapp.MainActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def tearDown(self):
        "Tear down the test"
        self.driver.quit()

    def test_maaii(self):
        #"Testing the ATP WTA app "
        self.driver.implicitly_wait(30)
        time.sleep(5)

        print ("Slept for 5 sec...")

        element = self.driver.find_element_by_name('7')

#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Android_Maaii)
    unittest.TextTestRunner(verbosity=2).run(suite)

错误报告:

ERROR: test_maaii (__main__.Android_Maaii)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python34\testfiles\MaaiiTest_new.py", line 59, in test_maaii
    element = self.driver.find_element_by_name('7')
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 302, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
    raise wde
  File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
    super(MobileErrorHandler, self).check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: 'An unknown server-side error occurred while processing the command.' 

======================================================================
ERROR: test_maaii (__main__.Android_Maaii)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python34\testfiles\MaaiiTest_new.py", line 31, in tearDown
    self.driver.quit()
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 455, in quit
    self.execute(Command.QUIT)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
    super(MobileErrorHandler, self).check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 138, in check_response
    if 'message' in value:
TypeError: Type str doesn't support the buffer API

----------------------------------------------------------------------
Ran 1 test in 47.363s

FAILED (errors=2)

真正的问题是什么?我尝试了所有的定位元素方法,但都失败了。

【问题讨论】:

    标签: python webdriver element appium locate


    【解决方案1】:

    我在 Python 中是 0,我使用 Java,但我认为我最近遇到了同样的问题。 我可以找到所有元素,与它们一起行动,但无法按下按钮。要查找元素,请使用 Appium Inspector。

    剩下的 - This 是我的问题,已经解决了。看看下面的解决方案。

    希望这会有所帮助。 ;)

    【讨论】:

      【解决方案2】:

      尝试使用元素的 resource_id 而不是文本。如果您在 ui automator viewer 中查看,您将看到元素的以下属性。例如,计算器的数字 7 有 com.android.calculator2:id/digit7 作为 resource_id。

      所以使用可以在你的代码中使用它: element = self.driver.find_element_by_id("com.android.calculator2:id/digit7)

      【讨论】:

        【解决方案3】:

        您可以使用 android SDK 中的 uiautomatorviewer 来获取每个元素的 id,您可以使用元素的 id。 然后使用

         self.driver.implicitly_wait(10)
         self.driver.find_element_by_id("com.android.calculator2:id/digit7").click()
         self.driver.implicitly_wait(10)
        

        【讨论】:

        • 没有看到resource-id,因为我的设备是Android 4.2.2。
        • 似乎 api 级别 > 18 则可以获取资源 ID。我可以让您的 Skype 或其他联系人联系吗?急需帮助。
        【解决方案4】:

        您可以使用 appium 服务器并使用 Appium 服务器实例中的“启动 Inspector Session”按钮,如下所示(圈出)。

        然后通过填充所需的功能并使用“开始会话”启动会话来启动要检查的活动(定位元素),如下所示

        一旦您从使用此启动的预期 Activity 获得资源 id。使用以下方法,您可以在 Python 中使用资源 ID:

        self.driver.find_element_by_id("resourceid").click()
        

        确保 Appium 服务器在运行测试时正在运行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-10
          • 1970-01-01
          • 2018-03-15
          • 2019-09-08
          • 2021-05-03
          • 1970-01-01
          • 2014-07-27
          • 1970-01-01
          相关资源
          最近更新 更多