简体   繁体   English

如何使用Appium和Python查找元素?

[英]How to locate elements with Appium and Python?

  • Testing a simple Calculator App on Android Device. 在Android设备上测试一个简单的计算器应用。
  • Tried many methods to locate an element, but failed. 尝试了许多方法来查找元素,但是失败了。
  • The information I can get from UI Automator Viewer is the class name, button name, co-ordinates. 我可以从UI Automator Viewer中获得的信息是类名称,按钮名称,坐标。 I just want to locate the button "7" and press it. 我只想找到按钮“ 7”并按下它。

Codes: 代码:

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 Report: 错误报告:

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)

What is the real problem? 真正的问题是什么? I have tried all locate element methods but failed. 我已经尝试了所有的定位元素方法,但是都失败了。

You can use uiautomatorviewer present in android SDK to get the id of each element, the you can use the id of the element. 您可以使用android SDK中存在的uiautomatorviewer来获取每个元素的ID,您可以使用该元素的ID。 Then use 然后使用

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

在此处输入图片说明

Try to use the resource_id of the element instead of the text. 尝试使用元素的resource_id而不是文本。 If you view in ui automator viewer, you will see the following properties for an element. 如果在ui automator查看器中查看,则将看到元素的以下属性。 For example, number 7 of the calculator has, com.android.calculator2:id/digit7 as resource_id. 例如,计算器的数字7具有com.android.calculator2:id / digit7作为resource_id。

So use can use this in your code as: element = self.driver.find_element_by_id("com.android.calculator2:id/digit7) 因此使用可以在您的代码中将其用作:element = self.driver.find_element_by_id(“ com.android.calculator2:id / digit7)

I am 0 in Python, I use Java but I think I had the same problem recently. 我在Python中为0,我使用Java,但我认为我最近也遇到了同样的问题。 I could find all elements, act with them, but couldn't press the button. 我可以找到所有元素,并与他们一起行动,但无法按下按钮。 To find elements use Appium Inspector. 要查找元素,请使用Appium Inspector。

And for the rest - This is my problem, already solved. 其余的- 是我的问题,已经解决了。 look the solution under. 看下解决方案。

hope this helps. 希望这可以帮助。 ;) ;)

You can use appium server and use "Start Inspector Session" button from the Appium server instance as shown below(encircled). 您可以使用appium服务器,并使用Appium服务器实例中的“启动Inspector会话”按钮,如下所示(带圆圈)。

在此处输入图片说明

Then launch the Activity to be inspected (locate elements) by filling desired capabilities and starting the session using "Start Session" as shown below 然后通过填充所需的功能并使用“开始会话”启动会话来启动要检查的活动(定位元素),如下所示

在此处输入图片说明

Once you obtain resource id from the intended Activity launched using this. 一旦从使用此方法启动的预期活动中获取资源ID。 Using following you can work with the resource ids in Python: 使用following可以在Python中使用资源ID:

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

Make sure the Appium server is running when running the test. 运行测试时,请确保Appium服务器正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM