【Appium踩坑】Mitmproxy脚本+Appium启动后,手机无网络问题

1、解决方法

关闭wifi,重新开启

2、关闭wifi、开启wifi方法封装

from appium.webdriver.connectiontype import ConnectionType

def set_no_connection(driver):
    """
    设置成-无网络
    :param driver:
    :return:
    """
    driver.set_network_connection(ConnectionType.NO_CONNECTION)
    return driver

def set_wifi(driver):
    """
    设置成-开启wifi
    :param driver:
    :return:
    """
    driver.set_network_connection(ConnectionType.WIFI_ONLY)
    return driver

3、开启 or 关闭 wifi ,对应的appium 日志查看

// 开wifi, 没开数据流量+飞行模式

# 关闭 wifi

2021-12-29 03:24:25:558 - [HTTP]
2021-12-29 03:24:25:563 - [HTTP] --> POST /wd/hub/session/8738855f-50ab-40e9-a361-6e021465870f/network_connection
2021-12-29 03:24:25:563 - [HTTP] {"parameters":{"type":0}}

2021-12-29 03:24:25:568 - [debug] [W3C (8738855f)] Calling AppiumDriver.setNetworkConnection() with args: [0,"8738855f-50ab-40e9-a361-6e021465870f"]
2021-12-29 03:24:25:568 - [AndroidDriver] Setting network connection
2021-12-29 03:24:25:568 - [AndroidDriver] Getting network connection
2021-12-29 03:24:25:569 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global airplane_mode_on'
2021-12-29 03:24:25:593 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global wifi_on'
2021-12-29 03:24:25:617 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global mobile_data'
2021-12-29 03:24:25:639 - [AndroidDriver] Not changing airplane mode, since it is already disabled
2021-12-29 03:24:25:641 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell am broadcast -a io.appium.settings.wifi -n io.appium.settings/.receivers.WiFiConnectionSettingReceiver --es setstatus disable'
2021-12-29 03:24:25:681 - [AndroidDriver] Not changing data connection state, since it is already disabled
2021-12-29 03:24:25:682 - [AndroidDriver] Getting network connection
2021-12-29 03:24:25:682 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global airplane_mode_on'
2021-12-29 03:24:25:707 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global wifi_on'
2021-12-29 03:24:25:735 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global mobile_data'

2021-12-29 03:24:25:760 - [debug] [W3C (8738855f)] Responding to client with driver.setNetworkConnection() result: 0



# 开启 wifi
2021-12-29 03:26:52:920 - [HTTP] --> POST /wd/hub/session/0544bf54-173d-4009-beab-cf8b6877a7dc/network_connection
2021-12-29 03:26:52:920 - [HTTP] {"parameters":{"type":2}}

2021-12-29 03:26:52:923 - [debug] [W3C (0544bf54)] Calling AppiumDriver.setNetworkConnection() with args: [2,"0544bf54-173d-4009-beab-cf8b6877a7dc"]

2021-12-29 03:26:52:923 - [AndroidDriver] Setting network connection
2021-12-29 03:26:52:923 - [AndroidDriver] Getting network connection
2021-12-29 03:26:52:923 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global airplane_mode_on'
2021-12-29 03:26:52:993 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global wifi_on'
2021-12-29 03:26:53:058 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global mobile_data'
2021-12-29 03:26:53:094 - [AndroidDriver] Not changing airplane mode, since it is already disabled
2021-12-29 03:26:53:095 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell am broadcast -a io.appium.settings.wifi -n io.appium.settings/.receivers.WiFiConnectionSettingReceiver --es setstatus enable'
2021-12-29 03:26:53:129 - [AndroidDriver] Not changing data connection state, since it is already disabled
2021-12-29 03:26:53:130 - [AndroidDriver] Getting network connection
2021-12-29 03:26:53:130 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global airplane_mode_on'
2021-12-29 03:26:53:156 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global wifi_on'
2021-12-29 03:26:53:185 - [debug] [ADB] Running '/Users/xxxx/Library/Android/sdk/platform-tools/adb -P 5037 -s ed0335e2 shell settings get global mobile_data'

2021-12-29 03:26:53:217 - [debug] [W3C (0544bf54)] Responding to client with driver.setNetworkConnection() result: 2

4、这个方法有缺陷,解决方法见下面链接⬇️

使用Appium Setting开启wifi or 关闭wifi,弹系统wifi权限弹窗问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Appium是一个用于自动化移动应用程序的工具,它支持多种编程语言,包括Python。在使用Appium Python脚本连接手机之前,我们首先需要安装Appium服务器和Appium Python客户端库。 首先,我们需要确保已安装Appium服务器,并启动该服务器。然后,在Python环境中安装Appium Python客户端库。可以使用pip工具来安装Appium Python客户端库: ```python pip install Appium-Python-Client ``` 接下来,我们创建一个Python脚本来连接手机,并执行一些操作。在脚本中,我们需要指定设备的一些参数,如设备名称、平台版本、应用包名等。然后,我们可以使用Appium提供的API来进行手机操作,如启动应用、查找元素、模拟手势等。 一个简单的Appium Python脚本连接手机的例子如下: ```python from appium import webdriver desired_caps = { 'platformName': 'Android', 'platformVersion': '9.0', 'deviceName': 'emulator-5554', 'appPackage': 'com.example.myapp', 'appActivity': '.MainActivity' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 执行一些操作 element = driver.find_element_by_id('com.example.myapp:id/button') element.click() # 关闭应用 driver.quit() ``` 以上代码示例中,我们使用了Appium提供的webdriver.Remote方法连接手机,并指定了设备参数。然后,我们找到了应用中的一个按钮元素,并点击了它。最后,我们使用quit方法关闭了应用。 通过编写Appium Python脚本,我们可以方便地连接手机,并实现自动化测试、应用操作等功能。同时,Appium提供了丰富的API和文档,可以帮助我们更好地进行移动应用自动化开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值