对于自定义键盘无法像系统键盘那样使用Send Keys进行输入,于是接下来想到的是把键盘当做普通控件处理,直接点击进行输入,以下面app界面为例
在文本框中输入股票代码“600004”
def test_ihexin03(self):
self.driver.find_element_by_accessibility_id("自选").click()
self.driver.find_element_by_accessibility_id("搜索").click()
sleep(2)
# 点击键盘输入股票代码“600004”
self.driver.find_element_by_accessibility_id("6").click()
self.driver.find_element_by_accessibility_id("0").click()
self.driver.find_element_by_accessibility_id("0").click()
self.driver.find_element_by_accessibility_id("0").click()
self.driver.find_element_by_accessibility_id("0").click()
self.driver.find_element_by_accessibility_id("4").click()
sleep(5)
终端报错
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #f5f5f5; background-color: #000000}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #f5f5f5; background-color: #000000; min-height: 14.0px}span.s1 {font-variant-ligatures: no-common-ligatures}
HeminWon-Extranet:~ Hm$ python /Users/Hm/Desktop/ITestSwipe0713.py
test_ihexin03 (__main__.ComplexIOSTests) ... ERROR
======================================================================
ERROR: test_ihexin03 (__main__.ComplexIOSTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Hm/Desktop/ITestSwipe0713.py", line 69, in test_ihexin03
self.driver.find_element_by_accessibility_id("6").click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "build/bdist.macosx-10.11-intel/egg/appium/webdriver/errorhandler.py", line 29, in check_response
raise wde
WebDriverException: Message: An unknown server-side error occurred while processing the command.
----------------------------------------------------------------------
Ran 1 test in 12.284s
FAILED (errors=1)
appium关键日志
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #00f900; background-color: #000000}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #ffffff; background-color: #000000; min-height: 14.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #ffffff; background-color: #000000}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #fffb00; background-color: #000000}span.s1 {color: #00fdff}span.s2 {color: #ffffff}span.s3 {color: #00f900}span.s4 {color: #ff2600}
info: [debug] [INST] 2016-07-20 09:15:23 +0000 Debug: responding with:
info: [debug] [INST] 2016-07-20 09:15:23 +0000 Debug: Running system command #8: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":{"ELEMENT":"2"}}...
info: [debug] Socket data received (38 bytes)
info: [debug] Socket data being routed.
info: [debug] Got result from instruments: {"status":0,"value":{"ELEMENT":"2"}}
info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"2"},"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2"}
info: <-- POST /wd/hub/session/5dbfa274-884f-4a91-8c57-dd325bcab8c2/element 200 1451.300 ms - 87 {"status":0,"value":{"ELEMENT":"2"},"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2"}
info: --> POST /wd/hub/session/5dbfa274-884f-4a91-8c57-dd325bcab8c2/element/2/click {"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2","id":"2"}
info: [debug] Pushing command to appium work queue: "au.tapById('2')"
info: [debug] Sending command to instruments: au.tapById('2')
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai MobileGestaltHelper[90] <Error>: libMobileGestalt MobileGestalt.c:281: server_access_check denied access to question UniqueDeviceID for pid 297
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai ScriptAgent[297] <Error>: libMobileGestalt MobileGestaltSupport.m:151: pid 297 (ScriptAgent) does not have sandbox access for re6Zb+zwFKJNlkQTUeT+/w and IS NOT appropriately entitled
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai ScriptAgent[297] <Error>: libMobileGestalt MobileGestalt.c:542: no access to UniqueDeviceID (see <rdar://problem/11744455>)
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai locationd[64] <Notice>: need a scan, count, 0, 0, lwatchdog, 0.0, interval, 60.0, needWatchdog, 0
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: Got new command 8 from instruments: au.tapById('2')
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: evaluating au.tapById('2')
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: UIAButton.tap()
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: UIAButton.scrollToVisible()
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: UIAButton - scrollToVisible cannot be used because this element has no scrollable ancestor.
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai MobileGestaltHelper[90] <Error>: libMobileGestalt MobileGestalt.c:281: server_access_check denied access to question UniqueDeviceID for pid 297
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai ScriptAgent[297] <Error>: libMobileGestalt MobileGestaltSupport.m:151: pid 297 (ScriptAgent) does not have sandbox access for re6Zb+zwFKJNlkQTUeT+/w and IS NOT appropriately entitled
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:24 oumeikai ScriptAgent[297] <Error>: libMobileGestalt MobileGestalt.c:542: no access to UniqueDeviceID (see <rdar://problem/11744455>)
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: UIAButton - scrollToVisible cannot be used because this element has no scrollable ancestor.
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Debug: UIAButton could not be tapped because the element is not visible
info: [debug] Socket data received (57 bytes)
info: [debug] Socket data being routed.
info: [debug] Got result from instruments: {"status":13,"value":"elementId 2 could not be tapped"}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"elementId 2 could not be tapped"},"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2"}
info: <-- POST /wd/hub/session/5dbfa274-884f-4a91-8c57-dd325bcab8c2/element/2/click 500 1140.832 ms - 200
info: --> DELETE /wd/hub/session/5dbfa274-884f-4a91-8c57-dd325bcab8c2 {}
info: Shutting down appium session
info: [debug] Stopping ios
info: [debug] Destroying instruments client socket.
info: [debug] Closing socket server.
info: [debug] Instruments socket server was closed
info: [debug] Sending sigterm to instruments
info: [debug] [INST] 2016-07-20 09:15:24 +0000 Error: VerboseError: elementId 2 could not be tapped
info: [debug] [INST] 2016-07-20 09:15:25 +0000 Error: Error during eval: [native code]
file:///Applications/Appium.app/Contents/Resources/node_modules/appium/47041F0C-3B9E-48EB-9705-9FF11C85BD86/bootstrap-5923cadea0343e3f.js:711:30
tapById@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/47041F0C-3B9E-48EB-9705-9FF11C85BD86/bootstrap-5923cadea0343e3f.js:1100:42
eval code
eval@[native code]
startProcessing@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/47041F0C-3B9E-48EB-9705-9FF11C85BD86/bootstrap-5923cadea0343e3f.js:2710:30
bootstrap@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/47041F0C-3B9E-48EB-9705-9FF11C85BD86/bootstrap-5923cadea0343e3f.js:2778:31
global code@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/47041F0C-3B9E-48EB-9705-9FF11C85BD86/bootstrap-5923cadea0343e3f.js:2787:10
info: [debug] [INST] 2016-07-20 09:15:25 +0000 Debug: responding with:
info: [debug] [INST] 2016-07-20 09:15:25 +0000 Stopped: Script was stopped by the user
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai SpringBoard[58] <Warning>: HW kbd: Failed to set (null) as keyboard focus
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai SpringBoard[58] <Warning>: HW kbd: Failed to set (null) as keyboard focus
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai SpringBoard[58] <Warning>: UNNotificationRegistrarConnectionListener connection invalidated
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai com.apple.xpc.launchd[1] (UIKitApplication:cn.com.10jqka.IHexin[0x6d56][296]) <Notice>: Service exited due to signal: Killed: 9
info: [debug] [INST] 2016-07-20 09:15:25 +0000 Debug: Running system command #9: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":13,"value":"elementId 2 could not be tapped"}...
info: [debug] [INST STDERR] 2016-07-20 17:15:25.608 instruments[3035:25965] Attempting to change event horizon while disengage
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai SpringBoard[58] <Warning>: Application 'UIKitApplication:cn.com.10jqka.IHexin[0x6d56]' exited abnormally via signal.
info: [IOS_SYSLOG_ROW ] Jul 20 17:15:25 oumeikai UserEventAgent[26] <Warning>: 60966505040: id=cn.com.10jqka.IHexin pid=296, state=0
info: [debug] [INST] Instruments Trace Complete (Duration : 42.755268s; Output : /tmp/appium-instruments/instrumentscli0.trace)
info: [debug] [INSTSERVER] Instruments exited with code 0
info: [debug] Cleaning up after instruments exit
info: [debug] Stopping iOS log capture
info: [debug] Running ios sim reset flow
info: [debug] Killing the simulator process
info: [debug] Killing any other simulator daemons
info: [debug] Killall iOS Simulator
info: [debug] On a real device; cannot clean device state
info: [debug] Cleaning up appium session
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2"}
info: <-- DELETE /wd/hub/session/5dbfa274-884f-4a91-8c57-dd325bcab8c2 200 1703.757 ms - 76 {"status":0,"value":null,"sessionId":"5dbfa274-884f-4a91-8c57-dd325bcab8c2"}
info: --> GET /wd/hub/status {}
问题原因?
解决方法:不能直接tap元素,只能tap对应的坐标才会有效,所以选择封装一个tap的方法
class CustomDriver(webdriver.Remote):
def hx_tap_element(self, element):
location = element.location
el_size = element.size
return self.tap([(el_size['width'] / 2 + location['x'], el_size['height'] / 2 + location['y'],)])
def hx_tap_element_with_name(self, name):
el = self.find_element_by_name(name=name)
return self.hx_tap_element(el)
def hx_tap_element_with_name_sequence(self, sequence):
return [self.hx_tap_element_with_name(name=name) for name in sequence]
def hx_tap_element_with_name_args(self, *args):
return [self.hx_tap_element_with_name(name=name) for name in args]
def test_ihexin03(self):
self.driver.find_element_by_name("交易").click()
self.driver.find_element_by_name("模拟炒股").click()
#self.driver.find_element_by_name("买入").click()
self.driver.find_element_by_xpath(
" //UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIATableView[1]/UIATableCell[3]/UIAButton[1]").click()
sleep(2)
self.driver.find_element_by_xpath(
"//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAScrollView[1]/UIATextField[1]").click()
# sleep(2)
# self.driver.hx_tap_element_with_name_sequence(('全仓', '1',))
self.driver.hx_tap_element_with_name_sequence('600000')
self.driver.find_element_by_xpath(
"//UIAApplication[1]/UIAWindow[1]/UIAScrollView[1]/UIAScrollView[1]/UIATextField[3]").click()
sleep(2)
self.driver.hx_tap_element_with_name_args('全仓', '半仓', '2/3仓')