更新下我的最新代码,我发现可以不用切换 webview 但是,,这种办法是依赖 content-desc 属性值,也不太靠谱稳定。但是我找不到更好的办法,因为在尝试切所有的 webview 后 我找打印内容中均未发现我需要的内容,难道 driver.page_source 这句有问题 并不能输出页面完成内容? 下面为我的最新代码:
import sys
import os
import unittest
from time import sleep
from appium import webdriver
if name=='main':
#unittest.main()
desired_caps={}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.0.0'
#desired_caps['platformVersion'] = '7.1.1'
desired_caps['appPackage']='com.tencent.mm'
desired_caps['appActivity']='.ui.LauncherUI'
#desired_caps['deviceName']='b955905d' #vivo 手机
#desired_caps['recreateChromeDriverSessions'] = True 来回切换 webview
desired_caps['deviceName']='UYT0218323001687'# 华为手机
#desired_caps['deviceName'] = 'ac27da9b' # 小米手机
#desired_caps['app']=''
desired_caps["fastReset"]='False'
desired_caps['fullReset']='False' # 通过卸载程序的方式重置程序
desired_caps['noReset']='true' #Session 开始之前不重置程序状态
desired_caps['automationName']='appium'
desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:appbrand1'}
#desired_caps['unicodeKeyboard']=True # 是否支持 unicode 键盘,输入中文设置 true
#desired_caps['resetKeyboard']=True # 是否重置键盘
url="http://127.0.0.1:4723/wb/hub"
driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub,desired_caps")
sleep(3)
print('进入微信点击发现前')
print(driver.contexts)
print(driver.current_context)
button1=driver.find_element_by_xpath("//[@text='发现']")
button1.click()
sleep(3)
print('进入微信点击发现后')
print(driver.contexts)
print(driver.current_context)
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
print(x,y)
driver.swipe(x/2,y*0.25,x/2,y*0.05,1000)
sleep(3)
driver.find_element_by_xpath("//[@text='小程序']").click()
print('点击小程序后')
print(driver.contexts)
print(driver.current_context)
sleep(10)
driver.tap([(255,529),(459,598)],500)
#driver.find_element_by_xpath("//[contains(@text,'美团外卖')]").is_displayed()
#driver.find_element_by_xpath("//[contains(@text,'美团外卖')]").click()
sleep(10)
print('点击美团后')
print(driver.page_source)
print(driver.contexts)
print(driver.current_context)
#driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand1')
sleep(10)
print('ok!!!!')
print(driver.current_context)
#print(driver.find_element_by_xpath("//"))
print(driver.page_source)
driver.find_element_by_xpath("//[contains(@content-desc,'美食')]").click()
#driver.find_element_by_xpath("//[contains(@text,'美食')]").click()
# driver.find_element_by_xpath("//[contains(@text,'首页')]").click()
# driver.find_element_by_xpath("/html/body/wx-view/wx-view/wx-swiper[1]/div/div[1]/div/wx-swiper-item[1]/wx-view[1]/wx-image/div").click()
print("已点")
sleep(5)
driver.quit()
我昨天发现,点击美团小程序后不切换 webview,直接打印页面内容 print(driver.page_source) 如下:
....." bounds="[0,330][1080,876]" resource-id="" instance="12"><android.view.View index="1" text="" class="android.view.View" package="com.tencent.mm" content-desc="异国料理" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false.......
内容太长 我只截取一部分。
然后我要点击美团首页中的美食按钮,则使用 driver.find_element_by_xpath("//*[contains(@content-desc,'美食')]").click(),
可以完成点击。
在我切换 webview 后,所有可用 webview 内容均使用上面方法打印,未发现可用内容。
我的疑点是,现在做小程序自动化测试不需要切换 webview 了?不过上面打印页面内容 看不懂 不像网页 html 等等 如果使用 content-desc 这个标签就感觉不太靠谱 等等 疑点。
我使用华为 mate10 手机时候报错 谷歌浏览器不可达 即上面回答问题。在使用 vivo 手机和同样代码跑,就可以切换 webview,但是还找不到里面的元素,可能手机问题,希望能够帮助其他人, 但是我在使用 vivo 时候他找不到小程序里美团外卖的按钮,我使用的坐标点击策略目前,但是不是长久之计。
请问 谷歌浏览器不可达 问题 解决了吗
这个报错是我切换 webview 时候报错
我知道什么原因了,里面的还是需要切换 webview 元素。
不知道设置了哪里 我发现我不需要切换 webview 了 可以直接操作小程序里页面!!之前还一直找切换 webview 报错提示谷歌浏览器不可达,找原因找了好久 哈哈哈最后发现不知道怎么弄的设置 居然不需要切换 webview 了,代码如下:import sys
import os
import unittest
from time import sleep
from appium import webdriver
'''class TestAndorid(unittest.TestCase):
def setUp(self):
print('ready!')
def test_A(self):
print('A')
def tearDown(self):
print('end!')
'''
if name=='main':
#unittest.main()
desired_caps={}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.0.0'
desired_caps['appPackage']='com.tencent.mm'
desired_caps['appActivity']='.ui.LauncherUI'
#desired_caps['deviceName']='b955905d' #vivo 手机
desired_caps['deviceName']='UYT0218323001687'# 华为手机
#desired_caps['deviceName'] = 'ac27da9b' # 小米手机
desired_caps['app']=''
desired_caps["fastReset"]='False'
desired_caps['fullReset']='false' # 通过卸载程序的方式重置程序
desired_caps['noReset']='true' #Session 开始之前不重置程序状态
desired_caps['automationName']='appium'
desired_caps['chromeOptions']={'androidProcess':'com.tencent.mm:tools'}
#desired_caps['unicodeKeyboard']=True # 是否支持 unicode 键盘,输入中文设置 true
#desired_caps['resetKeyboard']=True # 是否重置键盘
url="http://127.0.0.1:4723/wb/hub"
driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub,desired_caps")
sleep(3)
print('进入微信点击发现前')
print(driver.contexts)
print(driver.current_context)
button1=driver.find_element_by_xpath("//[@text='发现']")
button1.click()
sleep(3)
print('进入微信点击发现后')
print(driver.contexts)
print(driver.current_context)
x=driver.get_window_size()['width']
y=driver.get_window_size()['height']
print(x,y)
driver.swipe(x/2,y*0.25,x/2,y*0.05,1000)
sleep(3)
driver.find_element_by_xpath("//[@text='小程序']").click()
print('点击小程序后')
print(driver.contexts)
print(driver.current_context)
sleep(5)
driver.find_element_by_xpath("//[contains(@text,'美团外卖')]").click()
sleep(30)
print('点击美团后')
print(driver.contexts)
print(driver.current_context)
# driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
print('ok!!!!')
sleep(5)
driver.find_element_by_xpath("//[@text='订单']").click()
print("已点")
sleep(5)
driver.quit()
为什么 我安装最新的 appium desktop v1.6 版本的 安装能够完成,但是运行 exe 文件 无反应 也不弹框提示,执行 appium-doctor 也提示都是 ok 的
[Chromedriver] Set chromedriver binary as: C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
[Chromedriver] Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (netstat -nao ^| findstr /R /C:"8000 "
) do (FOR /F "usebackq" %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe
) do (IF NOT %b=="" TASKKILL /F /PID %a))
[Chromedriver] No old chromedrivers seemed to exist
[Chromedriver] Cleaning any old adb forwarded port socket connections
[ADB] List forwarding ports
[ADB] Running 'D:\test\AndroidSdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","UYT0218323001687","forward","--list"]
[ADB] Removing forwarded port socket connection: 12608
[ADB] Running 'D:\test\AndroidSdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","UYT0218323001687","forward","--remove","tcp:12608"]
[Chromedriver] Spawning chromedriver with: C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[Chromedriver] Chromedriver version: '2.28.455520'
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"6.1.7601 SP1\"}}}"
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"UYT0218323001687"}}}
[JSONWP Proxy] Got response with status 200: {"sessionId":"6bc8ba69baf82753c45100b73b2364c6","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"UYT0218323001687"}}}
[JSONWP Proxy] Got response with status 200: {"sessionId":"ab360dae53ce766dced1e8fc6c9a9ea1","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"UYT0218323001687"}}}
[JSONWP Proxy] Got response with status 200: {"sessionId":"04ec4c7df9cc160429ecf94f041ccbc4","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64))
at Object.wrappedLogger.errorAndThrow (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
at Chromedriver.callee$2$0$ (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
at tryCatch (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Users\xxq\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at
弄了很久还没解决
楼主解决了吗
楼主解决了吗?