You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Python移动自动化脚本遇Selenium授权错误求助

解决Appium移动自动化脚本运行时的"Authorization required"错误

运行DriverScript.py时触发selenium.common.exceptions.WebDriverException: Message: Authorization required错误,完整报错堆栈如下:

PS C:\Users\Shibin.R\Downloads\retail-east-connectedcustomer-main\retail-east-connectedcustomer-main> python DriverScript.py
Traceback (most recent call last):
File "C:\Users\Shibin.R\Downloads\retail-east-connectedcustomer-main\retail-east-connectedcustomer-main\DriverScript.py", line 62, in 
mobile=Mobile(platform,report,script,device,argDict["testData"]["location"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Shibin.R\Downloads\retail-east-connectedcustomer-main\retail-east-connectedcustomer-main\FunctionLibrary\Generic\Mobile\GenericFunctions.py", line 146, in __init__
self.driver = webdriver.Remote(
^^^^^^^^^^^^^^^^^
File "C:\Users\Shibin.R\AppData\Roaming\Python\Python311\site-packages\appium\webdriver\webdriver.py", line 257, in __init__
super().__init__(
File "C:\Users\Shibin.R\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__
self.start_session(capabilities)
File "C:\Users\Shibin.R\AppData\Roaming\Python\Python311\site-packages\appium\webdriver\webdriver.py", line 346, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, w3c_caps)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Shibin.R\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
self.error_handler.check_response(response)
File "C:\Users\Shibin.R\AppData\Roaming\Python\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 193, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: Authorization required

已确认所有依赖包安装完成,问题仍未解决。


解决步骤

  • 检查Appium Server的认证配置
    如果Appium Server启用了基础认证,初始化webdriver.Remote时需传入认证信息。修改GenericFunctions.py中初始化driver的代码:

    from base64 import b64encode
    
    # 替换为实际的Appium认证账号密码
    username = "your_appium_username"
    password = "your_appium_password"
    auth_str = b64encode(f"{username}:{password}".encode()).decode()
    
    # 初始化driver时添加认证请求头
    self.driver = webdriver.Remote(
        command_executor="http://your_appium_server:4723/wd/hub",
        desired_capabilities=your_capabilities,
        headers={"Authorization": f"Basic {auth_str}"}
    )
    
  • 关闭Appium Server的不必要认证
    若测试环境不需要认证,重新启动Appium Server时移除--username和--password参数:

    appium --port 4723
    
  • 排查网络代理/防火墙限制
    确认脚本运行环境的代理未拦截Appium Server请求,或将Appium Server地址加入代理白名单;同时检查本地防火墙是否允许脚本访问Appium的监听端口(默认4723)。

  • 验证版本兼容性
    确保Appium Python Client与Selenium版本匹配,比如Selenium 4.x对应Appium Python Client 2.x及以上。可重新安装指定版本:

    pip uninstall appium-python-client selenium
    pip install appium-python-client==2.11.0 selenium==4.15.2
    
  • 检查设备调试权限
    真机测试时,确认设备已授权电脑进行USB调试(在设备上确认"允许USB调试"弹窗);模拟器测试时,确认模拟器正常启动且端口未被占用,执行adb devices验证设备连接状态。


内容的提问来源于stack exchange,提问作者Shibin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 02:10:30