简体   繁体   English

如何修复 Python Appium 自动化的“UiAutomator2 服务器无法启动,因为本地端口 #4729 正忙”?

[英]How to fix "UiAutomator2 Server cannot start because the local port #4729 is busy" for Python Appium automation?

Environment:环境:

Windows 10 Python 3.7.9 Appium 1.22.3 Node v12.15.0 Android Debug Bridge version 1.0.41 Version 33.0.2-8557947 Sdkmanager 4.0.1 Appium-Python-Client==1.2.0 Windows 10 Python 3.7.9 Appium 1.22.3 Node v12.15.0 Android Debug Bridge 版本 1.0.41 版本 33.0.2-8557947 Sdkmanager 4.0.1 Appium-Python-Client==1.2.0

Problem:问题:

I am trying to run smartphone automation on several devices and I get these error messages: Python exception :我正在尝试在多个设备上运行智能手机自动化,但收到以下错误消息: Python 异常:

can't set attribute

Appium log: Appium日志:

[UiAutomator2] UiAutomator2 Server cannot start because the local port #4729 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.

UnknownError: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4729 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
    at getResponseForW3CError (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
    at asyncHandler (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)

What did I try:我尝试了什么:

I kill the node process before running appium to be sure there aren't previous sessions open.我在运行 appium 之前终止了节点进程,以确保没有以前的会话打开。 I check with netstat command if ports were occupied (netstat -anp | find ":4726").我用 netstat 命令检查端口是否被占用(netstat -anp | find ":4726")。 Everything seems correct.一切似乎都是正确的。

Here is the code to reproduce the issue (you can change the app packages with anything you want):这是重现问题的代码(您可以使用任何您想要的内容更改应用程序包):

def MakeSomething(p_udid, systemPort, devicename, platformversion, OS_device):
    print(50*"*")
    print(p_udid)
    print(systemPort)
    print(devicename)
    print(platformversion)
    print(OS_device)
    print(50 * "*")
    try:
        p_driver= Initialisation_Driver(p_udid, systemPort, devicename, platformversion, OS_device)
        result=ClickMenu(p_driver)
    except Exception as ex:
        print(f"ERROR with MakeSomething: {ex}")

def ClickMenu(p_driver):

        menu_btn = WebDriverWait(p_driver, 10).until(EC.presence_of_element_located((By.XPATH, "//android.view.View[contains(@content-desc,'Menu')]")))
        if menu_btn is not None:
            menu_btn.click()
            p_driver.implicitly_wait(10)
            time.sleep(random.uniform(1.1, 2.1))
            return True
        else:
            return False



def Initialisation_Driver(p_udid, p_systemPort, p_deviceName, p_version, p_os):
    # ======================== INITIALISATION OF DRIVER ================================
    print(
        f"{p_udid}|||============== INITIALISATION OF DRIVER for Smartphone {p_udid}  ==========")
    print(f"{p_udid}|||p_udid : {p_udid}")
    print(f"{p_udid}|||p_systemPort : {p_systemPort}")
    print(f"{p_udid}|||p_deviceName : {p_deviceName}")
    print(f"{p_udid}|||p_version : {p_version}")
    print(f"{p_udid}|||p_os : {p_os}")
    print(
        f"{p_udid}|||===============================================================================================")

    desired_caps = {}
    desired_caps['automationName'] = 'UiAutomator2'

    desired_caps['platformName'] = p_os
    desired_caps['platformVersion'] = p_version
    desired_caps['deviceName'] = p_deviceName
    desired_caps['udid'] = p_udid
    desired_caps['noReset'] = 'true'
    desired_caps['systemPort'] = p_systemPort
    desired_caps['chromeDriverPort'] = p_systemPort
    desired_caps['appWaitDuration'] = 100000
    desired_caps['newCommandTimeout'] = 0
    desired_caps['wdaStartupRetries'] = 4
    desired_caps['wdaStartupRetryInterval'] = 20000
    desired_caps['uiautomator2ServerLaunchTimeout'] = 100000
    desired_caps['uiautomator2ServerInstallTimeout'] = 100000
    desired_caps['remoteAppsCacheLimit'] = 0
    desired_caps['waitForQuiescence'] = 'false'
    # desired_caps['appWaitPackage'] = 'com.facebook.android'
    # desired_caps['appWaitActivity'] = '.StartActivity'
    desired_caps['appPackage'] = 'com.facebook.katana'
    desired_caps['appActivity'] = 'com.facebook.katana.LoginActivity'
    cpt_appium_start = 0

    try:
        p_driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        print(f"desired_caps['appPackage'] : {desired_caps['appPackage']}")
        print(f"desired_caps['appActivity'] : {desired_caps['appActivity']}")
        # p_driver.update_settings({"normalizeTagNames": True})
        time.sleep(random.uniform(3.5, 5.3))
        return p_driver

    except Exception as ex:

        print(f"{p_udid}|||Something went wrong when initializing p_driver : {ex}")



# kill previous appium
PROCNAME_NODE = "node.exe"
for proc in psutil.process_iter():
    if proc.name() == PROCNAME_NODE:
        # print(f"PhoneBot will kill this process : {proc.name}")
        proc.kill()
        print(f"{PROCNAME_NODE} has been killed!!!")

# Start Appium
proc = subprocess.Popen(f'appium --log appium.log --log-level error:debug --debug-log-spacing --async-trace',
                            shell=True,
                            stdin=None, stdout=None, stderr=None, close_fds=True)


# List of smartphones with its attributes
list_smartphones_connected=[
    ('A60ProEEA0112572','4725','A60Pro_EEA','Android','9.0'),
    ('E531XE1ZM11300644','4726','BV5500S_EEA','Android','10.0'),
    ('E531XE1ZM11302266','4727','BV5500S_EEA','Android','10.0'),
    ('OUKIC16PRO37851 ','4728','C16_Pro_EEA','Android','9.0')
]

# open a thread for each smartphone
for smartphone_connected in list_smartphones_connected:
    p_udid= smartphone_connected[0]
    systemPort= smartphone_connected[1]
    devicename= smartphone_connected[2]
    OS_device= smartphone_connected[3]
    platformversion= smartphone_connected[4]



    thread_smartphone = threading.Thread(target=MakeSomething,
                                         args=[p_udid, systemPort, devicename, platformversion, OS_device])
    thread_smartphone.start()
    print(f"thread_smartphone started : {thread_smartphone}")

Here are the Python logs:以下是 Python 日志:

C:\Users\gauth\AppData\Local\Programs\Python\Python37\python.exe C:/Users/gauth/Documents/PhoneBot/Phonebot_debug3/test.py
p_file : log.log
result : log.log
p_file : log.log
result : log.log
p_file : log.log
result : log.log
p_file : log.log
result : log.log
**************************************************thread_smartphone started : <Thread(Thread-1, started 3848)>
A60ProEEA0112572
4725

A60Pro_EEA
9.0
Android
**************************************************
A60ProEEA0112572|||============== INITIALISATION OF DRIVER for Smartphone A60ProEEA0112572  ==========
A60ProEEA0112572|||p_udid : A60ProEEA0112572
A60ProEEA0112572|||p_systemPort : 4725
A60ProEEA0112572|||p_deviceName : A60Pro_EEA
A60ProEEA0112572|||p_version : 9.0
A60ProEEA0112572|||p_os : Android
A60ProEEA0112572|||===============================================================================================
**************************************************thread_smartphone started : <Thread(Thread-2, started 3624)>
E531XE1ZM11300644

4726
BV5500S_EEA
10.0
Android
**************************************************
E531XE1ZM11300644|||============== INITIALISATION OF DRIVER for Smartphone E531XE1ZM11300644  ==========
E531XE1ZM11300644|||p_udid : E531XE1ZM11300644
E531XE1ZM11300644|||p_systemPort : 4726
E531XE1ZM11300644|||p_deviceName : BV5500S_EEA
E531XE1ZM11300644|||p_version : 10.0
E531XE1ZM11300644|||p_os : Android
E531XE1ZM11300644|||===============================================================================================
**************************************************
E531XE1ZM11302266
4727
BV5500S_EEA
10.0
Android
**************************************************
E531XE1ZM11302266|||============== INITIALISATION OF DRIVER for Smartphone E531XE1ZM11302266  ==========
E531XE1ZM11302266|||p_udid : E531XE1ZM11302266
E531XE1ZM11302266|||p_systemPort : 4727
E531XE1ZM11302266|||p_deviceName : BV5500S_EEA
E531XE1ZM11302266|||p_version : 10.0
E531XE1ZM11302266|||p_os : Android
E531XE1ZM11302266|||===============================================================================================
thread_smartphone started : <Thread(Thread-3, started 10368)>
**************************************************thread_smartphone started : <Thread(Thread-4, started 7132)>
OUKIC16PRO37851 

4728
C16_Pro_EEA
9.0
Android
**************************************************
OUKIC16PRO37851 |||============== INITIALISATION OF DRIVER for Smartphone OUKIC16PRO37851   ==========
OUKIC16PRO37851 |||p_udid : OUKIC16PRO37851 
OUKIC16PRO37851 |||p_systemPort : 4728
OUKIC16PRO37851 |||p_deviceName : C16_Pro_EEA
OUKIC16PRO37851 |||p_version : 9.0
OUKIC16PRO37851 |||p_os : Android
OUKIC16PRO37851 |||===============================================================================================
[AndroidDriver] Device OUKIC16PRO37851  was not in the list of connected devices
OUKIC16PRO37851 |||Something went wrong when initializing p_driver : Message: An unknown server-side error occurred while processing the command. Original error: Device OUKIC16PRO37851  was not in the list of connected devices
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: Device OUKIC16PRO37851  was not in the list of connected devices
    at getResponseForW3CError (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
    at asyncHandler (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
ERROR with MakeSomething: 'NoneType' object has no attribute 'find_element'
[UiAutomator2] UiAutomator2 Server cannot start because the local port #4725 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
[UiAutomator2] UiAutomator2 Server cannot start because the local port #4727 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
[UiAutomator2] UiAutomator2 Server cannot start because the local port #4726 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
A60ProEEA0112572|||Something went wrong when initializing p_driver : Message: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4725 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4725 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
    at getResponseForW3CError (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
    at asyncHandler (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
ERROR with MakeSomething: 'NoneType' object has no attribute 'find_element'
E531XE1ZM11302266|||Something went wrong when initializing p_driver : Message: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4727 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4727 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
    at getResponseForW3CError (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
    at asyncHandler (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
ERROR with MakeSomething: 'NoneType' object has no attribute 'find_element'
E531XE1ZM11300644|||Something went wrong when initializing p_driver : Message: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4726 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: UiAutomator2 Server cannot start because the local port #4726 is busy. Make sure the port you provide via 'systemPort' capability is not occupied. This situation might often be a result of an inaccurate sessions management, e.g. old automation sessions on the same device must always be closed before starting new ones.
    at getResponseForW3CError (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
    at asyncHandler (C:\Users\gauth\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
ERROR with MakeSomething: 'NoneType' object has no attribute 'find_element'

Process finished with exit code 0

Here is the appium log:这是appium日志:

https://github.com/gauthierbuttez/public/blob/master/appium_issue04082020.log https://github.com/gauthierbuttez/public/blob/master/appium_issue04082020.log

Does anyone have any idea of what is going on please?请问有人知道发生了什么吗?

Ok.好的。 I fixed it.我修好了它。 In case someone one day faces the same issue, you need to check the version of different packages.万一有一天有人遇到同样的问题,你需要检查不同包的版本。 In my case, I had a very recent version of all packages except Appium-Python-Client.就我而言,我拥有除 Appium-Python-Client 之外的所有软件包的最新版本。 I had to upgrade this Appium-Python-Client to the latest version:我不得不将此 Appium-Python-Client 升级到最新版本:

Appium-Python-Client==2.4.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中以编程方式启动 appium 服务器 - How to start appium server programmatically in Python 如何使用 AppiumService() 从我的 python 代码启动 appium 服务器? - How to start appium server from my python code with AppiumService()? uiautomator2.exceptions.GatewayError 使用 uiautomator2 时 - uiautomator2.exceptions.GatewayError while using uiautomator2 如何使用Appium / Python在真实设备上启动应用程序? - How to start an application on real device with Appium/Python? 如何修复错误自动化游戏 Python 代码 - How To Fix Error Automation Game Python Code 如何修复发送UDP套接字的本地端口 - How to fix local port of sending UDP socket 如何通过python调用appium server - How to invoke the appium server via python 如何修复“致命 Python 错误:_enter_buffered_busy:无法获取 &lt;_io.BufferedWriter name=”的锁定<stdout> '&gt; 在解释器关闭时' 错误?</stdout> - How to fix a 'fatal Python error: _enter_buffered_busy: could not aquire lock for <_io.BufferedWriter name='<stdout>'> at interpreter shutdown' error? Python3无法连接错误? 怎么修? - Python3 Cannot concatenate error? How to fix? 在VS代码中,由于某种原因,我无法运行任何python文件,因为它在cmd中写入python而不是py。 反正有解决办法吗? - In VS code, for some reason i cannot run any python file because it writes python instead of py in cmd. is there anyway to fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM