Appium 新手学 appium-python-appium 的 hybrid 应用如何获取 native 跟 webview 的值

· May 27, 2015 · Last by kaige201314 replied at February 02, 2016 · 1493 hits

最近搞 hybrid 应用的脚本,搞了很长时间。。。。还是没搞完全,但是怎么获得 native 跟 webview 的值,我是会了。。。测试机型 4.4.4,大家可以看看,然后试试,给我反馈。。。然后如果有切换到 webview 后能顺利操作的同学给我看看你们的代码。。。我实在是搞得头痛了~资料比较少~

#coding:utf-8
from appium import webdriver
from time import sleep

desired_caps = {}
#desired_caps['automationName'] = 'selendroid'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = '192.168.56.101:5555'
#desired_caps['app'] = 'Calculator.apk'
desired_caps['appPackage'] = 'com.travelsky.bluesky'
desired_caps['appActivity'] = '.UIView'

dr = webdriver.Remote('http://192.168.10.59:4723/wd/hub', desired_caps)

sleep(3)
dr.implicitly_wait(10)
#获取contexts,打印出来查看
a=dr.contexts
print (a)
dr.implicitly_wait(10)
#dr.switch_to_window("WEBVIEW_com.travelsky.bluesky");
dr.switch_to.context("WEBVIEW_com.travelsky.bluesky")
#脚本进行到这一步,我不清楚什么问题,还是请大神帮我看看~
'''code=self.dr.find_element_by_id("serviceCode")
code.clear()
code.send_keys("test")'''
sleep(3)
dr.close_app()
dr.quit()

看看我的截图吧:

划红的地方就是我们获取的 contexts 了,包含了 native 跟 webview。

另外怎么切,还有问题,最好大神好心帮我看看。。。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 9 条回复 时间 点赞

默默收藏

2Floor has deleted

错误代码贴全一点吧

#4 · May 27, 2015 Author

#3 楼 @anikikun

['NATIVE_APP', 'WEBVIEW_com.travelsky.bluesky']
Traceback (most recent call last):
  File "D:\appium\ccarehybridtest.py", line 22, in <module>
    dr.switch_to.context("WEBVIEW_com.travelsky.bluesky")
  File "C:\Python34\lib\site-packages\appium\webdriver\switch_to.py", line 31, in context
    self._driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {'name': context_name})
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 417, in _request
    resp = opener.open(request)
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1210, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1185, in do_open
    r = h.getresponse()
  File "C:\Python34\lib\http\client.py", line 1171, in getresponse
    response.begin()
  File "C:\Python34\lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "C:\Python34\lib\http\client.py", line 313, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Python34\lib\socket.py", line 374, in readinto
    return self._sock.recv_into(b)
[Decode error - output not utf-8]

别在 st2 上 ctrl+b,试试用 cmd 直接 run

#6 · May 27, 2015 Author

#5 楼 @anikikun 效果一样的,一直这样,报错信息,是我强行关掉 appium 的服务窗口出现的

这个问题没有解决么

        context=self.driver.contexts
self.driver.switch_to.context('WEBVIEW')
el=self.driver.find_element_by_id('userName')
el.click

我的代码片断如上,供楼主参考。新手来学习

但换成 WEBVIEW 后,无法用 SENDKEY 对文本框输入,求高手指导

请问楼主,您现在解决这个问题了吗。如果解决了,请帖一下代码吧,我也被折磨好久了

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