報錯:
Call to 'sendKeys' failed [element.sendKeys("1345678xxxx")] Error response ·····
原因是輸入框控件是android.view.View,只能點擊,不能sendkeys發送內容
輸入框控件如下圖:
解決方法:
使用driver.press_keycode(number)
其中number為數字,代表不同按鍵,具體如下:
keycode 4:返回鍵(Back key)
keycode 5:電話鍵(Call key)
keycode 6:結束通話鍵(End Call key)
keycode 7-16:依次為數字0-9
keycode 17:*
keycode 18:#
keycode 19-23:上、下、左、右、中間
keycode 24-25:音量上、下
keycode 26:電源鍵(Power key)
keycode 27:相機鍵(Camera key)
keycode 28:清除鍵(Clear key)
keycode 29-54:字母A-Z
keycode 55:,
keycode 56:.
keycode 61:Tab鍵(Tab key)
keycode 62:空格鍵(Space key)
keycode 66:回車鍵(Enter key)
keycode 67:退格鍵(Backspace key)
keycode 68:`
keycode 69:-
keycode 70:=
keycode 71:[
keycode 72:]
keycode 73:\
keycode 74:;
keycode 75:'
keycode 76:/
keycode 77:@
keycode 81:+
keycode 82:菜單鍵(Menu key)
keycode 84:搜索鍵(Search key)
keycode 164:靜音鍵(Volume Mute key)
keycode 7-16:依次為數字0-9,所以使用時可以自定義一個字典,譬如這里需要輸入的是手機號,定義一個num字典,其中key為數字,value為對應的按鍵
num= {'0': 7, '1': 8, '2': 9, '3': 10, '4': 11, '5': 12, '6': 13, '7': 14, '8': 15, '9': 16}
self.find_element(*loc.mobile_loc, '點擊手機號輸入框').click()
mobile = '1343456xxxx' for i in mobile: if num.get(i): #通過key獲取value self.driver.press_keycode(num[i]) #按壓對應的鍵進行輸入 self.driver.keyevent(61) #tab鍵