Appium 【已解决】driver.find_element_by_id()报错

qiuqiu · 2014年06月22日 · 最后由 rhyme 回复于 2014年06月24日 · 3224 次阅读

el = driver.find_element_by_id("editText")
使用 find_element_by_id() 函数报错,程序中确实有此 id 控件,求大神指导
appium 报错:

info: [BOOTSTRAP] [debug] Finding container using ID with the contextId:
info: [BOOTSTRAP] [info] Returning result: {"value":"Could not find an element using supplied strategy. ID editText doesn't exist as text or content desc.","status":7}
info: Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Could not find an element using supplied strategy. ID editText doesn't exist as text or content desc."},"sessionId":"aa94ae49-4817-495a-8c94-8aa42a7f4586"}


python-test:
from selenium import webdriver
import os
import unittest

PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
)
desired_caps = {}
#desired_caps['device'] = 'android'
#desired_caps['browserName'] = ''
#desired_caps['version'] = '4.4.2'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.2'
desired_caps['deviceName'] = '192.168.1.104:5554'
desired_caps['app'] = PATH(
'./test.apk'
)
desired_caps['app-package'] = 'com.example.test'
desired_caps['app-activity'] = '.MainActivity'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
el = driver.find_element_by_name("5")
el.click()
el = driver.find_element_by_name("2")
el.click()
el = driver.find_element_by_name("+")
el.click()
el = driver.find_element_by_name("1")
el.click()
el = driver.find_element_by_id("editText")
result = el.getText()
unittest.assertEqual(result, '521')

driver.quit()


android-xml:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" >
android:layout_width="match_parent"
android:layout_height="match_parent" >


android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:cursorVisible="false"
android:editable="false"
android:gravity="right|center_vertical"
android:lines="1"
android:textSize="32sp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="32sp" />
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"
android:textSize="32sp" />
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="32sp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
android:id="@+id/num7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
android:textSize="32sp" />
android:id="@+id/num8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
android:textSize="32sp" />
android:id="@+id/num9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
android:textSize="32sp" />
android:id="@+id/subtract"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
android:id="@+id/num4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
android:textSize="32sp" />
android:id="@+id/num5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
android:textSize="32sp" />
android:id="@+id/num6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
android:textSize="32sp" />
android:id="@+id/multiply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="32sp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
android:id="@+id/num1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="32sp" />
android:id="@+id/num2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
android:textSize="32sp" />
android:id="@+id/num3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
android:textSize="32sp" />
android:id="@+id/divide"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="32sp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:textSize="32sp" >
android:id="@+id/num0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textSize="32sp" />
android:id="@+id/point"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="."
android:textSize="32sp" />
android:id="@+id/negative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="32sp" />
android:id="@+id/equal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="32sp" />

共收到 8 条回复 时间 点赞

等 1.2 版本解决

el = driver.find_element_by_id("editText") 这个 ID 你不能看源码里的 ID 的,这个 id 跟源码里对应的是 content_des

如果是 1.1 的话, 现在 by id 好像改了, 用这个 by_class_name('android.widget.EditText') 试试, 然后好像也没有 getText 了, 直接.text 就行

#2 楼 @eric content_des 是什么属性,在哪里可以查找到

#3 楼 @cy_suncheng 是 1.1,我试了 el = driver.find_element_by_class_name('android.widget.editText') 还是报错

info: [BOOTSTRAP] [debug] Finding android.widget.editText using CLASS_NAME with the contextId:
info: [BOOTSTRAP] [info] Returning result: {"value":"No element found","status":7}
info: Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"43ff6502-43b1-4963-92f7-a2f479e68bce

#2 楼 @eric 找到了 content_desc 用 content_desc 的值放在 id 的位置调用,也还是报错

#6 楼 @qiuqiu 解决了,应该是 el = driver.find_element_by_id('com.example.test:id/editText')

#7 楼 @qiuqiu 你用的是 resource id 是可以, 按理说 classname 应该也可以的, 我一直用 class name, 你的 python client 是最新的么?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册