Appium TypeError: 'unicode' object can not be callable when using appium + python2.7 in eclipse + pydev environment.

liliu · 2014年04月20日 · 最后由 Kilmer 回复于 2015年04月22日 · 975 次阅读

我的测试代码如下。
import unittest
from selenium import webdriver

class Test(unittest.TestCase):
desired_caps = {}
def setUp(self):

self.desired_caps['device'] = 'Android'
self.desired_caps['browserName'] = ''
self.desired_caps['version'] = '4.2'
self.desired_caps['app'] = '/Users/apple/Downloads/spondroid-debug-unaligned.apk'
self.desired_caps['app-package'] = 'com.spond.spond'
self.desired_caps['app-activity'] = '.WelcomeActivity'

self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)

def tearDown(self):
self.driver.quit()

def testLogin(self):
driver = self.driver
loginButton =driver.find_element_by_name('Log in')
#to check if we can get the tag name through method WebElement.tag_name()
print('loginButton.tag_name():'+loginButton.tag_name())# print(type(loginButton.id()))
print(loginButton.id())
if loginButton.is_enabled():

print("loginButton.is_enabled():true")
else:
print('loginButton.is_enabled():false')
if loginButton.is_displayed():
print('loginButton.is_displayed():true')
else:
print('loginButton.is_displayed():false')
print('loginButton.text:'+loginButton.text)

if name == "main":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()

运行时报错:

======================================================================

ERROR: testLogin (android_login.Test)

Traceback (most recent call last):
File "/Users/apple/Documents/workspace/autoSpond/src/python/android_login.py", line 37, in testLogin
print('loginButton.tag_name():'+loginButton.tag_name())# print(type(loginButton.id()))
TypeError: 'unicode' object is not callable


Ran 1 test in 17.130s

FAILED (errors=1)

请各位大侠帮忙看看,这个问题怎么解决啊。先谢谢啦!

共收到 4 条回复 时间 点赞

自己代码的问题, 自己调试下吧

匿名 #2 · 2014年08月28日

TypeError: 'unicode' object is not callable 当遇到这样的错误时候, 一般是属性当做方法调用了,比如,selenium 脚本, driver.title 写成 driver.title()

'unicode' object is not callable

PYTHON 中遇到这种错误,指的是字符串被当做了函数使用,

tag_name 是 logButton 对象的函数还是属性,如果是属性,那么应该是 logButton.tag_name 不是 logButton.tag_name()

帮你查了一下源码:

@property
 def tag_name(self):
       """This element's ``tagName`` property."""
       return self._execute(Command.GET_ELEMENT_TAG_NAME)['value']

tag_name 为标志位了@property 了,也就是说 这个函数在外部访问的时候,可以将他当成属性来使用,所以正确的做法应该是 logButton.tag_name 不要加括号

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册