Appium [已解决] appium 使用 send_keys () 无法输入文字

jhw · January 20, 2016 · Last by wanlik700c replied at August 22, 2019 · 9419 hits

写了条case,从txt文件读取内容,用send_keys()输入到app的文本输入框中。可是每个环节好像都正确,最后case结果也ok,就是输入框中没有输入任何内容。请教各位大神这是怎么回事?

case:

def readFile(self,filename):
f = open(filename,"r")
lines = []
while True:
line = f.readline().decode("utf8")
if not line: break
lines.append(line)
f.close()
return "".join(lines)

def test_04_editBody(self):
text = self.readFile("text.txt")
print text
self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys(text)
self.driver.back()

txt文件的内容:

1. Into the Woods

"Help, A monster!” said Annie.
"
Yeah, sure!" said Jack. "A real monster in Frog Creek, Pennsylvania."
"
Run, Jack!" said Annie. She ran up the road.
Oh, brother.
This is what he got for spending time with his seven-year-old sister.
Annie loved pretend stuff. But Jack was eight and a half. He liked real things.
"
Watch out, Jack! The monster's coming! Race you!"
"
No, thanks! said Jack.
Annie raced alone into the woods. Jack looked at the sky. The sun was about to set.
"Come on, Annie! It's time to go home!"
But Annie had disappeared. Jack waited. No Annie.
"Annie!" he shouted again.
"Jack! Jack! Come here!"
Jack groaned.

(大概700个字符)

以下是log

info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]","context":"","multiple":false}]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.EditText[1] using XPATH with the contextId: multiple: false

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[CLASS=android.widget.EditText, INSTANCE=2]

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":{"ELEMENT":"2"}}
info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"2"},"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586"}
info: <-- POST /wd/hub/session/9d9a94a3-aafd-420c-af8c-675e105d4586/element 200 123.966 ms - 87 {"status":0,"value":{"ELEMENT":"2"},"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586"}
info: --> POST /wd/hub/session/9d9a94a3-aafd-420c-af8c-675e105d4586/element/2/value {"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586","id":"2","value":["1","."," ","I","n","t","o"," ","t","h","e"," ","W","o","o","d","s","\n"," "," "," "," "," ","\n"," "," "," "," "," ","\"","H","e","l","p",","," ","A"," ","m","o","n","s","t","e","r","!","”"," ","s","a","i","d"," ","A","n","n","i","e",".","\n"," "," "," "," "," ","\"","Y","e","a","h",","," ","s","u","r","e","!","\""," ","s","a","i","d"," ","J","a","c","k","."," ","\"","A"," ","r","e","a","l"," ","m","o","n","s","t","e","r"," ","i","n"," ","F","r","o","g"," ","C","r","e","e","k",","," ","P","e","n","n","s","y","l","v","a","n","i","a",".","\"","\n"," "," "," "," "," ","\"","R","u","n",","," ","J","a","c","k","!","\""," ","s","a","i","d"," ","A","n","n","i","e","."," ","S","h","e"," ","r","a","n"," ","u","p"," ","t","h","e"," ","r","o","a","d","."," ","\n"," "," "," "," "," ","O","h",","," ","b","r","o","t","h","e","r",".","\n"," "," "," "," "," ","T","h","i","s"," ","i","s"," ","w","h","a","t"," ","h","e"," ","g","o","
info: [debug] Pushing command to appium work queue: ["
element:setText",{"elementId":"2","text":"1. Into the Woods\n \n \"Help, A monster!” said Annie.\n \"Yeah, sure!\" said Jack. \"A real monster in Frog Creek, Pennsylvania.\"\n \"Run, Jack!\" said Annie. She ran up the road. \n Oh, brother.\n This is what he got for spending time with his seven-year-old sister.\n Annie loved pretend stuff. But Jack was eight and a half. He liked real things.\n \"Watch out, Jack! The monster's coming! Race you!\"\n \"No, thanks!” said Jack.\n Annie raced alone into the woods. Jack looked at the sky. The sun was about to set.\n \"Come on, Annie! It's time to go home!\"\n But Annie had disappeared. Jack waited. No Annie.\n \"Annie!\" he shouted again. \n \"Jack! Jack! Come here!\" \n Jack groaned.\n","replace":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:setText","params":{"elementId":"2","text":"1. Into the Woods\n \n \"Help, A monster!” said Annie.\n \"Yeah, sure!\" said Jack. \"A real monster in Frog Creek, Pennsylvania.\"\n \"Run, Jack!\" said Annie. She ran up the road. \n Oh, brother.\n This is what he got for spending time with his seven-year-old sister.\n Annie loved pretend stuff. But Jack was eight and a half. He liked real things.\n \"Watch out, Jack! The monster's coming! Race you!\"\n \"No, thanks!” said Jack.\n Annie raced alone into the woods. Jack looked at the sky. The sun was about to set.\n \"Come on, Annie! It's time to go home!\"\n But Annie had disappeared. Jack waited. No Annie.\n \"Annie!\" he shouted again. \n \"Jack! Jack! Come here!\" \n Jack groaned.\n","replace":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Using element passed in.
info: [debug] [BOOTSTRAP] [debug] Attempting to clear using UiObject.clearText().

info: [debug] [BOOTSTRAP] [debug] Text remains after clearing, but it appears to be hint text.

info: [debug] [BOOTSTRAP] [debug] Text not cleared. Assuming remainder is hint text.
info: [debug] [UIAUTOMATOR STDOUT] [APPIUM-UIAUTO] [debug] Sending plain text to element: 1. Into the Woods
info: [debug] [UIAUTOMATOR STDOUT] "Help, A monster!” said Annie.
info: [debug] [UIAUTOMATOR STDOUT] "
Yeah, sure!" said Jack. "A real monster in Frog Creek, Pennsylvania."
info: [debug] [UIAUTOMATOR STDOUT] "
Run, Jack!" said Annie. She ran up the road.
info: [debug] [UIAUTOMATOR STDOUT] Oh, brother.
info: [debug] [UIAUTOMATOR STDOUT] This is what he got for spending time with his seven-year-old sister.
info: [debug] [UIAUTOMATOR STDOUT] Annie loved pretend stuff. But Jack was eight and a half. He liked real things.
info: [debug] [UIAUTOMATOR STDOUT] "
Watch out, Jack! The monster's coming! Race you!"
info: [debug] [UIAUTOMATOR STDOUT] "
No, thanks! said Jack.
info: [debug] [UIAUTOMATOR STDOUT] Annie raced alone into the woods. Jack looked at the sky. The sun was about to set.
info: [debug] [UIAUTOMATOR STDOUT] "Come on, Annie! It's time to go home!"
info: [debug] [UIAUTOMATOR STDOUT] But Annie had disappeared. Jack waited. No Annie.
info: [debug] [UIAUTOMATOR STDOUT] "Annie!" he shouted again.
info: [debug] [UIAUTOMATOR STDOUT] "Jack! Jack! Come here!"
info: [debug] [UIAUTOMATOR STDOUT] Jack groaned.
info: [debug] [UIAUTOMATOR STDOUT] [/APPIUM-UIAUTO]

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":true}

info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586"}
info: <-- POST /wd/hub/session/9d9a94a3-aafd-420c-af8c-675e105d4586/element/2/value 200 6017.319 ms - 76 {"status":0,"value":true,"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586"}
info: --> POST /wd/hub/session/9d9a94a3-aafd-420c-af8c-675e105d4586/back {"sessionId":"9d9a94a3-aafd-420c-af8c-675e105d4586"}
info: [debug] Pushing command to appium work queue: ["pressBack"]

info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"pressBack","params":{}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: pressBack

实在不懂问题在哪,难道是字符串长度问题?

共收到 15 条回复 时间 点赞

没试过这么长的文字。短一点的可以成功吗?可以用set_value代替send_key,再试试。

mark down。。。

jhw #3 · January 20, 2016 作者

#2楼 @monkey 不好意思 之前没有认真排版 不知道这回可以了不

试试set_text()

jhw #5 · January 20, 2016 作者

#1楼 @b_88 短一点的文字可以输入,但是我想向文本框输入的文字越长越好。如果读一行输入一行的话,每次会把之前输入的删掉,达不到我要的效果。如果这个办法不行,不知还有没有其他办法?另外我用set_value会提示WebDriverException: Message: Not yet implemented. Please help us: http://appium.io/get-involved.html

#5楼 @jhwhale 可能知道你的问题原因了。你读txt文件后返回的不是字符串啊。我刚刚试了一下是可以大文本输入的。在返回的时候这样写,你试试看。

return "".join(lines).encode('ascii','ignore')

请问下 新手一直遇到 AttributeError: 'WebElement' object has no attribute 'sendkeys'的错误 怎么解决

#7楼 @xialuo 那WebElement到底有sendkeys这个func么~

#8楼 @andward 不知道, 脚本是和他们写的对比了下差不多,运行却报这个错

jhw #10 · January 21, 2016 作者

#6楼 @b_88 果然好了。谢谢!看来我还要加强学习。我又读了这两篇文章,感觉帮助很大,贴在这里 字符编码ASCII, Unicode和UTF-8概念扫盲Python编码介绍——encode和decode

好贴,收了

看完帖子,还是不太明白,我今天也遇到类似的问题了。我的计划是先输入帐号,然后再到密码文本框输入密码,用send_keys,结果运行起来时就变成:
1.先输入完整的帐号
2.在帐号文本框删掉帐号最后两位,然后接着在帐号文本框输入密码。。。
我今晚运行了4次,只有一次是按照我的期待成功运行,其他三次都是上面这种情况,太郁闷了。。。
我的appium版本是1.4.0.0

Moon [Topic was deleted] 中提及了此贴 15 Jul 17:51

好像 webdriver 的 send_keys 一次性能发 五六百的长度的字符

wanlik700c 回复

你好,请问你这问题解决了嘛,我也遇到了

P_Oliver 回复

你好,问题解决了吗?我解决这个问题的方法是:
在输入密码前,先在密码文本框点击一下,再做send_keys步骤。
希望能帮到你

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up