【问题标题】:Appium error : appium-python script cannot connect to the app, whereas Appium Desktop doesAppium 错误:appium-python 脚本无法连接到应用程序,而 Appium Desktop 可以
【发布时间】:2020-09-04 06:55:35
【问题描述】:

我正在尝试在 android 模拟器上自动执行一些任务,但遇到了一个非常奇怪的问题:我的 Appium + Python 脚本甚至无法启动应用程序,而 Appium Desktop 一切正常。

以下是详细信息:

首先,我启动我的 avd(使用第一个脚本):

date = datetime.now().strftime("%Y%m%d%H%M")
try:
    emu_process = subprocess.run("adb kill-server")
    print(emu_process)
except Exception as e:
    print("exception : {}".format(e))
    pass
emu_process = subprocess.Popen(["{}\emulator\emulator.exe".format(os.getenv('ANDROID_SDK_ROOT')),
                                "-avd", "{}".format(emu_model)])
emu_process = subprocess.run("adb start-server")

# Wait until the device is connected
time.sleep(2)
ready = False
while not ready:
    emu_process = subprocess.run("adb devices",
                                 capture_output=True, text=True)
    print(emu_process.stdout)
    print(emu_process.stdout[:4] == "List")


    if emu_process.stdout[:4] == "List" and \
            emu_process.stdout[-8:-2] == "device":
        ready=True
    time.sleep(2)

# store logcat on a .txt file
emu_process = subprocess.Popen(["adb", "logcat", "*:I", ">", 
                                "{}_adb.log".format(date)], shell=True)

然后,如果我在 localhost 上启动 Appium Desktop,端口 4723 具有所需的功能:

{
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "Android Emulator",
  "appPackage": "com.google.android.calculator",
  "appActivity": "com.android.calculator2.Calculator"
}

谷歌计算器启动。

但是,如果我启动以下脚本,脚本会卡在 webdriver.remote 行中:

from appium import webdriver
import subprocess
from datetime import datetime
from appium.webdriver.appium_service import AppiumService

# kill current node instance, to avoid multiple appium session
subprocess.Popen(['taskkill', '/F', '/IM', 'node.exe'])

ip_address = '127.0.0.1'
port = '4723'
date = datetime.now().strftime("%Y%m%d%H%M")

# Run Appium server, store logfile
appium_service = AppiumService()
appium_service.start(args=['--address', ip_address, '-p', port,
                           '--log-timestamp', '--log', 'D:\Temp\{}_appium.log'.format(date)])

print("appium is running : ", appium_service.is_running) # return True, ok
print("appium is listening : ", appium_service.is_listening) # return True, ok

desired_caps = {"platformName": "Android",
                "platformVersion": "10",
                "deviceName": "Android Emulator",
                "appPackage": "com.google.android.calculator",
                "appActivity": "com.android.calculator2.Calculator"
                }


driver = webdriver.Remote(
    command_executor='http://{}:{}/wd/hub'.format(ip_address, port),
    desired_capabilities=desired_caps
)

print("appium is connected to the device") # Nothing is printed, the script is stuck in the webdriver.remote line

你能帮我让这个脚本运行吗?在一周左右的时间里,我已经尝试了很多东西,但我不知道出了什么问题。

感谢您的帮助,

【问题讨论】:

    标签: python appium python-appium


    【解决方案1】:

    好的,我有答案了。 我似乎是一个错误,在这里引用: https://github.com/appium/python-client/issues/534

    解决方法是写

    appium_service.start(args=..., stdout=sp.DEVNULL)
    

    而不是:

    appium_service.start(args=...)
    

    希望其他人会像我一样高兴地找到这个答案:)

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 2018-12-27
      • 2018-06-13
      • 2021-03-18
      • 2016-06-03
      • 2022-11-08
      • 2022-12-22
      • 2015-09-25
      • 1970-01-01
      相关资源
      最近更新 更多