5.Appium使用id属性定位

id定位是在所有定位方式中最简单、最准确的一种定位方式,id定位的常用方法有两个:find_element_by_id()和find_elements_by_id()

区别:

后一个是一个复数形式,如果页面中元素id是不重复的,就使用第一种方法;如果该页面中存在多个元素id相同的元素就要使用第二个方法,该方法返回的是一个元素列表,列表中列出了所有id为该值的所有元素

使用方法

element = driver.find_element_by_id('foo')

element.click()

 

elements = driver.find_elements_by_id('foo')

elements[1].click()

 

from appium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
def android_driver():
    desired_cap={}
    desired_cap['platformName']="Android"
    desired_cap['platformVersion']='9.0'
    desired_cap['deviceName']='HONOR9X'
    desired_cap['udid']='GDB6R19718023173'  #ip或者设备好
    desired_cap['appPackage']='com.ss.android.article.news'   #要启动的应用包名
    desired_cap['appActivity']='com.ss.android.article.news.activity.MainActivity'  #要启动的应用activity
    desired_cap['noReset']=True   #不需要重新登录
    driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)

    return driver

if __name__=="__main__":
    driver=android_driver()
    time.sleep(10)
    try:
        element = driver.find_element_by_id('com.ss.android.article.news:id/bzw')
    except NoSuchElementException:
        print("没有找到这个元素")
    else:
        print("已经找到这个元素了,点击他")
        element.click()

我这里查找的元素是发布按钮,头条的元素id不同版本是不同的,可能这个例子在你那可能就会输入没有找到这个元素

详情请点击 https://study.163.com/course/introduction/1209833813.htm 查看

原创文章 100 获赞 339 访问量 56万+

猜你喜欢

转载自blog.csdn.net/MATLAB_matlab/article/details/105791984