Appium 关于 unittest 中的元素定位问题

zujie · 2017年02月13日 · 最后由 JoTsai 回复于 2017年02月16日 · 1835 次阅读

测试环境

  • appium1.5.3
  • python3
  • ios9.3
  • iPhone6s(模拟器)

场景

参考@huanzhijin老师发布的 Appium Python 中文版 API https://testerhome.com/topics/3711,进行元素定位,点击

这是用 inspector 给出的 APP 里面要定位的元素的具体信息,如图:

问题描述

1. find_element_by_accessibility_id

文章里的说明
find_element_by_accessibility_id
find_element_by_accessibility_id(self, id):

Finds an element by accessibility id.
    通过accessibility id查找元素
    :Args:
     - id - a string corresponding to a recursive element search using the
     Id/Name that the native Accessibility options utilize

    :Usage:
        driver.find_element_by_accessibility_id()
用法driver.find_element_by_accessibility_id(id)
我的代码:
self.driver.find_element_by_accessibility_id('个人').click()
Error
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: elementId 0 could not be tapped
  • 本来是用 find_element_by_name 的,在别的文章上面说 1.5 以后弃用了,得用 driver.find_element_by_accessibility_id() 代替,@huanzhijin老师文章里提到的是用 find_element_by_xpath 代替,结果还是报错

2. find_element_by_xpath

文章里的说明
find_element_by_xpath(self, xpath):

Finds element by xpath.
    通过Xpath定位元素详细方法可参阅http://www.w3school.com.cn/xpath/
    :Args:
        xpath - xpath of element to locate.  "//input[@class='myelement']"

    Note: The base path will be relative to this element's location.

    This will select the first link under this element.
    ::
        myelement.find_elements_by_xpath(".//a")

    However, this will select the first link on the page.
    ::
        myelement.find_elements_by_xpath("//a")

用法 find_element_by_xpath(“//*”)
我的代码
self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIATabBar[1]/UIAButton[4][contains(@text, '个人')]").click()
Error
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

几个疑问

  1. find_element_by_accessibility_id 里面的 ‘id’ 是 ‘name’ 吗,不是的话是哪个
  2. @huanzhijin前辈的文章中给出了好些个定位元素的方法,哪些是在 iOSAPP 上常用的
  3. 说个题外话,这样排版可以吗?逛 testerhome 很久了,最近才注册,第一次发帖,有没有注意到的地方,还请大家指正!
共收到 10 条回复 时间 点赞

@neyo 送子瞧瞧

accessibility_id 印象里拿的是 label,第一条错误信息不是元素没定位到,而是元素不可点,inspector 打开,左上角两个 fliter 的勾都去掉,再定位试试,还定位得到这个元素不

visible 是 false 的元素调用 click 方法会报这个错误,跟定位方法没有关系

#2 楼 @neyo

是这里吗勾掉吗, 还是不行,用 accessibility_id 和 xpath 都报和之前一样的错误

#3 楼 @JoTsai 那请问我应该怎样定位到元素并完成点击呢

#4 楼 @xiguamaomaoqiu 我的意思是勾去掉,再刷新下定位,你能看到 ‘个人’ 这个元素吗?

#6 楼 @neyo


对比一下, 去掉和不去掉, 怎么第二栏不见了这么多东西, 原来的 UIATabBar 也没了, 做这个验证是为了?

#7 楼 @xiguamaomaoqiu 去除不可操作的元素,干扰项。

#8 楼 @neyo
所以这里类型是 'UIButton' 的 '个人' 不见了,所以用'accessibility_id' 和 'xpath'还是点不到吗, 是开发那边的原因吗? 或者我这边怎么解决这个问题?😟

#5 楼 @xiguamaomaoqiu 要么让开发解决一下 visible 的问题,要么点击坐标

zujie 关闭了讨论 09月08日 14:30
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册