A brief introduction to Python Appium element positioning method

Source: Internet
Author: User

Python Appium element Positioning

Eight common positioning methods (common with selenium)

#ID Locatordriver.find_element_by_id ()#Name LocatorDriver.find_element_by_name ()#XPath positioningDriver.find_element_by_xpath ()#class_name PositioningDriver.find_element_by_class_name ()#CSS PositioningDriver.find_element_by_css_selector ()#Link PositioningDriver.find_element_by_link_text ()#tag PositioningDriver.find_element_by_tag_name ()#Partial_link PositioningDriver.find_element_by_partial_link_text ()

        # new ways        to move the end # accessibility_id Positioning         driver.find_element_by_accessibility_id ()        #  Android        driver.find _element_by_android_uiautomator ()        #  iOS        driver.find_element_by_ios_ UIAutomation ()

ID Locator

# the Resrouce-id property is ID:driver.find_element_by_id ('username' )

Name Locator

# the Text property is the name driver.find_element_by_name (U' Please enter user name ')

XPath positioning

For example, use Uiautomatorviewer.bat to get page elements

Note: The SDK comes with a uiautomatorviewer.bat that cannot get XPath and needs to replace a jar package to get the XPath. This jar is placed in QQ group:193056556, there is a need to get into the group to pick up.

Self.driver.find_element_by_xpath ("//android.widget.textview[@text = ']"). Click ()

Class_name positioning

Self.driver.find_element_by_class_name ('android.view.View'). Click ()

(Note: Generally a page of the class attribute is not unique, the element is not unique, the location will be error)

accessibility_id positioning

Content-desc is the corresponding accessibility_id

driver.find_element_by_accessibility_id (U" my "). Click ()

Android_uiautomator Positioning

Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")' ). Clear ()

The following is a code instance

#Test Exception Login-user does not exist    defTest_anouserlogin (self): driver=self.driver Time.sleep (5) driver.find_element_by_accessibility_id (U"my own"). Click () time.sleep (5) driver.find_element_by_accessibility_id (U"Login/Register"). Click () time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Send_keys ('Xzbuyer1') Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Send_keys ('111111') Time.sleep (2) driver.find_element_by_accessibility_id (U"Login"). Click () time.sleep (18) Get_alertmessage= driver.find_element_by_id ('Android:id/message'). Text Time.sleep (2)        ifGet_alertmessage = = U'user does not exist':            Print(U"the user does not exist and the use case executes successfully! ")        Else:            Print(U"use case execution failed! ")    defTest_bnopasslogin (self): driver=self.driver Time.sleep (5) driver.find_element_by_accessibility_id (U"my own"). Click () time.sleep (5) driver.find_element_by_accessibility_id (U"Login/Register"). Click () time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Send_keys ('Xzbuyer') Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Send_keys ('1111112') Time.sleep (2) driver.find_element_by_accessibility_id (U"Login"). Click () time.sleep (18) Get_alertmessage= driver.find_element_by_id ('Android:id/message'). Text Time.sleep (2)        Print(Get_alertmessage[0:5])        #if Get_alertmessage = = U ' user does not exist ':        #print U "user does not exist, use case execution succeeds!" "        #Else:        #print U "use case execution failed!" "        #    #Test Normal Login    defTest_clogin (self): driver=self.driver Time.sleep (5) driver.find_element_by_accessibility_id (U"my own"). Click () time.sleep (5) driver.find_element_by_accessibility_id (U"Login/Register"). Click () time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("username")'). Send_keys ('Xzbuyer') Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Clear () Time.sleep (2) Driver.find_element_by_android_uiautomator ('new Uiselector (). ResourceId ("password")'). Send_keys ('111111') Time.sleep (2) driver.find_element_by_accessibility_id (U"Login"). Click () time.sleep (2)        #Determine if login is successful        Print(DRIVER.FIND_ELEMENT_BY_ACCESSIBILITY_ID (U"Tibet Pharmaceutical Sales Company (test buyer)"). Get_attribute ('name')) Nickname= driver.find_element_by_accessibility_id (U"Tibet Pharmaceutical Sales Company (test buyer)"). Get_attribute ('name') Temp= Nickname[0:2]        Print(temp)ifNickname = = U'Tibet Pharmaceutical Sales Company (test buyer)':            Print('Login Successful! ')        Else:            Print('Login failed! ')    defTearDown (self): Self.driver.close_app () self.driver.quit ()

A brief introduction to Python Appium element positioning method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.