appium ConnectionRefusedError,urllib3.exceptions.NewConnectionError,urllib3.exceptions.MaxRetryError踩坑
报错关键信息

ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000014A42376898>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=9904): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000014A42376898>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))

报错的代码是

print(f"即将启动appium, 当前设备ID: {device_id}, 数据变量ID: {data_id}, 端口号: {port}")
print(f'appium -p {port}')
subprocess.Popen('appium -p %s' % port, shell=True)
# subprocess.call('appium -p %s' % port, shell=True)  # subprocess.call相当于subprocess.Popen().wait()
print('----appium server已启动----')
# time.sleep(5)d_capabilities = {"platformName": "Android","deviceName": "127.0.0.1:7555","appPackage": "com.tencent.mm","appActivity": ".ui.LauncherUI"
}driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % port, d_capabilities)

先说报错原因
subprocess创建子进程来启动appium server,appium启动需要时间,这个可以在命令启动看一下,会有好多日志,也就是说启动appium其实做了很多事(后面会写一篇启动和连接日志解析),但是当前主进程不会停下来,执行完subprocess.Popen这一行不等待立马就往下走了,然后走到webdriver.Remote这一行去连接appium server,可是此时appium还没启动完成,然后就抛出上面的异常堆栈

预防办法
这里要补充一下subprocess库的知识点
subprocess.Popen是创建一个异步非阻塞的子进程然后就不管了,主子进程之间不需要交互通信,可以说是创建完子进程就任其自生自灭。上面的代码执行起来是这样的

D:\Python37\python.exe D:/myproject/yiyan_v/fs/case/t4.py
现在在执行t4脚本
即将启动appium, 当前设备ID: 3, 数据变量ID: 2, 端口号: 9903
appium -p 9903
----appium server已启动----
[Appium] Welcome to Appium v1.22.3
[Appium] Non-default server args:
[Appium]   port: 9903
[Appium] Appium REST http interface listener started on 0.0.0.0:9903
[debug] [HTTP] Request idempotency key: ed2388e5-35fe-415f-b61b-0c41251ca71b
[HTTP] --> POST /wd/hub/session
[HTTP] {"capabilities":{"alwaysMatch":{"platformName":"Android","appium:deviceName":"127.0.0.1:7555","appium:appPackage":"com.tencent.mm","appium:appActivity":".ui.LauncherUI"},"firstMatch":[{}]},"desiredCapabilities":{"platformName":"Android","deviceName":"127.0.0.1:7555","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI"}}
[debug] [W3C] Calling AppiumDriver.createSession() with args: [{"platformName":"Android","deviceName":"127.0.0.1:7555","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI"},null,{"alwaysMatch":{"platformName":"Android","appium:deviceName":"127.0.0.1:7555","appium:appPackage":"com.tencent.mm","appium:appActivity":".ui.LauncherUI"},"firstMatch":[{}]}]
[debug] [BaseDriver] Event 'newSessionRequested' logged at 1649387378713 (11:09:38 GMT+0800 (GMT+08:00))
[Appium]
[Appium] ======================================================================
[Appium]   DEPRECATION WARNING:
[Appium]
[Appium]   The 'automationName' capability was not provided in the desired
[Appium]   capabilities for this Android session
[Appium]
[Appium]   Setting 'automationName=UiAutomator2' by default and using the
[Appium]   UiAutomator2 Driver
[Appium]
[Appium]   The next major version of Appium (2.x) will **require** the
[Appium]   'automationName' capability to be set for all sessions on all
[Appium]   platforms
[Appium]
[Appium]   In previous versions (Appium <= 1.13.x), the default was
[Appium]   'automationName=UiAutomator1'
[Appium]
[Appium]   If you wish to use that automation instead of UiAutomator2, please
[Appium]   add 'automationName=UiAutomator1' to your desired capabilities
[Appium]
[Appium]   For more information about drivers, please visit
[Appium]   http://appium.io/docs/en/about-appium/intro/ and explore the
[Appium]   'Drivers' menu
[Appium]
[Appium] ======================================================================
[Appium]
[Appium] Appium v1.22.3 creating new AndroidUiautomator2Driver (v1.70.1) session
[debug] [BaseDriver] W3C capabilities and MJSONWP desired capabilities were provided
[debug] [BaseDriver] Creating session with W3C capabilities: {
[debug] [BaseDriver]   "alwaysMatch": {
[debug] [BaseDriver]     "platformName": "Android",
[debug] [BaseDriver]     "appium:deviceName": "127.0.0.1:7555",
[debug] [BaseDriver]     "appium:appPackage": "com.tencent.mm",
[debug] [BaseDriver]     "appium:appActivity": ".ui.LauncherUI"
[debug] [BaseDriver]   },
[debug] [BaseDriver]   "firstMatch": [
[debug] [BaseDriver]     {}
[debug] [BaseDriver]   ]
[debug] [BaseDriver] }
[BaseDriver] Session created with session id: 6e0647ae-0ed0-4c2e-ac05-5df81598102b
[UiAutomator2] Starting 'com.tencent.mm' directly on the device
[debug] [UiAutomator2] Deleting UiAutomator2 session
[debug] [BaseDriver] Event 'newSessionStarted' logged at 1649387382016 (11:09:42 GMT+0800 (GMT+08:00))
[debug] [W3C] Encountered internal error running command: Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
[debug] [W3C]     at requireSdkRoot (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-adb\lib\helpers.js:36:11)
[debug] [W3C]     at Function.createADB (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-adb\lib\adb.js:57:23)
[debug] [W3C]     at Object.createBaseADB (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-android-driver\lib\android-helpers.js:129:20)
[debug] [W3C]     at Object.getDeviceInfoFromCaps (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-android-driver\lib\android-helpers.js:214:29)
[debug] [W3C]     at AndroidUiautomator2Driver.startUiAutomator2Session (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-uiautomator2-driver\lib\driver.js:338:40)
[debug] [W3C]     at AndroidUiautomator2Driver.createSession (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-uiautomator2-driver\lib\driver.js:229:18)
[debug] [W3C]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[debug] [W3C]     at AppiumDriver.createSession (D:\Program Files\nodejs\node_global\node_modules\appium\lib\appium.js:387:35)
Traceback (most recent call last):File "D:/myproject/yiyan_v/fs/case/t4.py", line 36, in <module>driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % port, d_capabilities)File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 275, in __init__
[HTTP] <-- POST /wd/hub/session 500 3557 ms - 875
[HTTP] AppiumConnection(command_executor, keep_alive=keep_alive), desired_capabilities, browser_profile, proxyFile "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 269, in __init__self.start_session(capabilities, browser_profile)File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 369, in start_sessionresponse = self.execute(RemoteCommand.NEW_SESSION, parameters)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in executeself.error_handler.check_response(response)File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more detailsat getResponseForW3CError (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)at asyncHandler (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)

这里会看到一个关键信息

UnknownError: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported.

如果你在安装appium时有用appium-doctor来检查并且显示OK,那么你就不要找错方向,自信点, ANDROID_HOME 和 ANDROID_SDK_ROOT 环境都没问题! 我猜测可能是因为appium还没完成启动,android和java环境变量还没加载到内存,然后代码就执行到webdriver.Remote了,也就是appium还没完成启动,就有访问的请求进来了,然后appium接收到请求去处理的时候发现java和android环境变量还没找到,所以报了这个错!


subprocess.call是创建一个阻塞的子进程,主进程和子进程之间需要交互通信,主进程会等待子进程的返回(命令执行成功返回0,命令执行报错返回1),通过返回判断后面代码的执行逻辑,所以如果使用subprocess.call来执行,那么执行日志就会看到是这样

现在在执行t4脚本
即将启动appium, 当前设备ID: 3, 数据变量ID: 2, 端口号: 9903
appium -p 9903
[Appium] Welcome to Appium v1.22.3
[Appium] Non-default server args:
[Appium]   port: 9903
[Appium] Appium REST http interface listener started on 0.0.0.0:9903

其实两个方式都没问题,如果单机可以直接使用call方法,可能你会问,那以后用例肯定要支持并发执行,所以并发的话还是要使用subprocess.Popen!

appium ConnectionRefusedError,urllib3.exceptions.NewConnectionError,urllib3.exceptions.MaxRetryError相关推荐

  1. python urllib3 使用_Python urllib3和代理

    我试图弄清楚如何使用代理和多线程. 此代码有效: requester = urllib3.PoolManager(maxsize = 10, headers = self.headers) threa ...

  2. python urllib3 使用_Python urllib3以及如何处理cookie支持?

    是不是有与多个Cookie的问题吗? 某些服务器返回多个Set-Cookie标题,但urllib3将标题存储在字典中,而字典不允许具有相同键的多个条目. httplib2有一个类似的问题. 或许不是: ...

  3. urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host=‘localhost‘, port=8097): Max retries excee

    问题描述: 使用nohup - > log.txt &命令训练深度学习模型时,输出日志报错Connection to remote host was lost- 相关命令可见我的另一篇博 ...

  4. 报错解决:RequestsDependencyWarning: urllib3 (1.24.3) or chardet (3.0.4) doesn't match a supported versio

    此文首发于我的个人博客:报错解决 RequestsDependencyWarning urllib3 (1.24.3) or chardet (3.0.4) doesn't match a suppo ...

  5. Appium+Python第一篇:appium的基本使用及启动APP

    一.appium的基本使用 appium环境安装 1.安装Android SDK (1)配置环境变量 用户变量的path添加以下三个路径: %ANDROID_HOME%\build-tools\31. ...

  6. 【Python网络爬虫】规则#20181023

    2019独角兽企业重金招聘Python工程师标准>>> Requests库安装 C:\Windows\System32>pip install requests Require ...

  7. 【代码】网易云音乐(周杰伦-晴天)评论的爬取

    参考爬取网易云音乐评论,典型的ajax加载,大多人是去破解js加密,有点繁琐. 爬取周杰伦-晴天这一条评论,因为数据量稍大.以后爬取整个歌手. 这是加密api的情况: import requestsd ...

  8. python request大批量发送请求调用接口时,报错:[WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。

    接到一个项目需求,其中需要调用到供应商的Http  API,因为有大量的测试资源,所以代码中会循环调用API. 然而在测试代码执行过程中,过程中偶尔报错: 此时看到报错,怀疑是可能是同时并发的问题, ...

  9. urllib3.exceptions.MaxRetryError问题的解决

    1.背景描述 在使用python+selenium做UI自动化时,单个脚本执行通过,多个脚本执行时,执行到第二个时失败.测试代码如下: # !/usr/bin/env python # -*- cod ...

最新文章

  1. jdbc mysql查询显示图片_在实现JDBC时如何显示存储引擎-MySQL CONNECTION查询?
  2. Android activity生命周期
  3. EdgeGallery — MECM — 系统架构
  4. oracle报12541错误,记录一次oracle无缘无故就无监听,报TNS-12541等错误的问题
  5. ubuntu下安装java和tomcat安装
  6. 好程序员大数据独家解析-hadoop五大节点
  7. 【leetcode❤python】Move Zeroes
  8. Shutter - 带有众多功能的屏幕截图工具
  9. iPhone 12s Pro渲染图曝光:屏幕和相机将成升级重点
  10. JAMStack-SSR/SSG 框架
  11. 几个Cache方案的比较
  12. vue 请求word文档流并打印预览
  13. 千千静听皮肤急速合成器
  14. 拼多多直播抽奖是什么?玩法介绍!
  15. 小暑 |入伏前,这样做,宝宝长高高更聪明!
  16. 批量打印二维码的软件---使用心得(领跑条码标签打印软件)
  17. EastWave应用案例:同轴线仿真
  18. Echart实现中国地图和地图打点
  19. 简单理解AutoResetEvent及ManualResetEvent
  20. Python兼职收益快赶上我本职工作了,一月收入7000+,记录一下

热门文章

  1. 深度增强学习(DRL)简单梳理
  2. 选择什么样的物联网卡才靠谱?
  3. php mysqli搜索数据库,PHP mysqli 操作数据库
  4. h264编码笔记(nal_ref_idc)
  5. c语言程序设计中数据类型有哪些,c语言中基本数据类型有哪些
  6. 2020-1-18学习总结
  7. 使用GSP动态修改SQL语句
  8. html控制页眉页脚,HTML到PDF解决方案(处理内容,页眉和页脚)
  9. 软件成本管理小资料之挣值分析
  10. Spring Boot 转 Vert.X 随笔