Appium appium 1.4+python 2.7.9 参考 android_contacts.py 无法输入中文,求举例

雨雾恨 · 2015年05月24日 · 最后由 雨雾恨 回复于 2015年05月24日 · 1774 次阅读

使用环境:appium 1.4 版本,python 2.7.9 在红米真机上运行 android_contacts.py,真机默认输入法是搜狗输入法中文 9 键
测试脚本如下:

#! /usr/bin/python
# -*- coding: utf8 -*- 
import os
import unittest
from appium import webdriver
from time import sleep

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.2'
        desired_caps['deviceName'] = 'Android Emulator'
        desired_caps['app'] = PATH(
            '../../../sample-code/apps/ContactManager/ContactManager.apk'
        )
        desired_caps['appPackage'] = 'com.example.android.contactmanager'
        desired_caps['appActivity'] = '.ContactManager'
        desired_caps["unicodeKeyboard"] = "True"
        desired_caps["resetKeyboard"] = "True"
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

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

    def test_add_contacts(self):
        el = self.driver.find_element_by_accessibility_id("Add Contact")
        el.click()

        textfields = self.driver.find_elements_by_class_name("android.widget.EditText")
        textfields[0].send_keys(u"您")
        textfields[2].send_keys("someone@appium.io")

        self.assertEqual('Appium User', textfields[0].text)
        self.assertEqual('someone@appium.io', textfields[2].text)

        self.driver.find_element_by_accessibility_id("Save").click()

        # for some reason "save" breaks things
        alert = self.driver.switch_to_alert()

        # no way to handle alerts in Android
        self.driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click()

        self.driver.press_keycode(3)



if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(ContactsAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

运行时,报错:

C:\Users\Youyu\Downloads\sample-code-master\sample-code\examples\python>python android_contacts.py
  File "android_contacts.py", line 35
SyntaxError: 'utf8' codec can't decode byte 0xc4 in position 0: invalid continuation byte

求指导,如何在 python 中能输入中文

共收到 25 条回复 时间 点赞

报的是SyntaxError,好好检查一个编码格式吧。
然后请正确使用代码块啊同学~

#1 楼 @anikikun textfields[0].send_keys(u"您") 就是不知道这行怎么错了,已经在开头引入了 utf-8

你得开头是这个 -*- coding: utf8 -*- ,非 utf-8

#3 楼 @xuxu 其实我自测过后发现 utf8 是管用的..

你文件真实保存编码确实是 utf-8 吗?

开头的编码声明是让解释器在遇到非 ASCII 编码时自动使用编码声明的编码来解码,并不是表示这个文件保存时就一定是 utf-8 。

要习惯性地把编辑器的默认编码都改成 utf-8.

-- coding: utf8 --这个是在网上抄录的,现在改为了 #coding=utf-8,运行脚本成立了,这个还有个前提是下载了 appium 自带输入法,这个输入法什么时候下载的还不是很明白

#7 楼 @yuwuhen333 desired_caps["unicodeKeyboard"] = "True"这一句代码起的作用,会帮你自动安装 Appium 的自带输入法来支持中文输入。

#8 楼 @anikikun 谢谢,学习了。上面的代码 #! /usr/bin/python

-- coding: utf8 -- 不改动,再次运行也通过了。

#10 楼 @mads 请问关于使用 python 定位 webview,该 webview 没有 resource-id 该如何定位呢

#12 楼 @mads Uiautomation 中没有 xpath 的值啊,怎么获取呢

#3 楼 @xuxu 关于 webview 的定位有什么好的方法吗?如果 webview 没有 id,怎么通过 xpath 或者其他来定位呢

#13 楼 @yuwuhen333 什么意思?uiautomatorview 还是 uiautomator,说清楚

#14 楼 @yuwuhen333 webview 是 HTML 格式,xpath,css,name,id 都可以定位

#15 楼 @mads uiautomatorview,Android sdk 中的 uiautomatorview.jar,如何进行反编译有了解吗?http://testerhome.com/topics/2632
这个中提到的对 uiautomatorview 进行二次开发,但是反编译这块还没搞明白

#17 楼 @yuwuhen333 不了解,webview 的定位,你可以先了解下 webdriver

你 webview 搞定没

#19 楼 @yuwuhen333 qq 多少来着?








大神门求助啊,输入中文编码一直弄不对,求指点,
eclipse 用的 64 位,jdk 用的 64 位,win7 64 位
输入一直是乱码,是因为 android 版本太高吗?


少传一张图,输入一直是乱码

#23 楼 @a63307517 最上方的改成 #coding=utf-8 这个放在第一行,试试。

#24 楼 @yuwuhen333 已解决,多谢回复

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