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

使用WinAppDriver与Selenium时遭遇TypeError错误求助

解决WinAppDriver + Selenium中desired_capabilities参数报错问题

你遇到的报错是因为Selenium 4.x版本已废弃desired_capabilities参数,该参数被替换为capabilities或options参数,传递旧参数会触发类型错误。

解决方案1:改用capabilities参数

直接将代码中的desired_capabilities参数名替换为capabilities即可:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Set up capabilities
capabilities = {
    "app": "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
}

# Initialize the WebDriver with WinAppDriver
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4723',
    capabilities=capabilities  # 替换参数名
)

try:
    # Wait for the app to open
    time.sleep(5)

    # Perform a simple calculation: 7 + 3 = 10
    driver.find_element(By.NAME, "Seven").click()
    driver.find_element(By.NAME, "Plus").click()
    driver.find_element(By.NAME, "Three").click()
    driver.find_element(By.NAME, "Equals").click()

    # Wait to see the result
    time.sleep(2)

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Close the application and quit the driver
    driver.quit()

解决方案2:使用WindowsOptions(Selenium 4推荐方式)

通过专门的Options类配置应用信息,这是Selenium 4的标准用法,可读性更强:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.windows.options import Options  # 导入WindowsOptions
import time

# 配置应用选项
options = Options()
options.app = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"

# Initialize the WebDriver with WinAppDriver
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4723',
    options=options  # 传递options参数
)

try:
    # Wait for the app to open
    time.sleep(5)

    # Perform a simple calculation: 7 + 3 = 10
    driver.find_element(By.NAME, "Seven").click()
    driver.find_element(By.NAME, "Plus").click()
    driver.find_element(By.NAME, "Three").click()
    driver.find_element(By.NAME, "Equals").click()

    # Wait to see the result
    time.sleep(2)

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Close the application and quit the driver
    driver.quit()

注意事项

  • 确保WinAppDriver已正常启动(默认端口4723)
  • 确认Selenium版本为4.x,若使用旧版Selenium 3.x则仍可使用desired_capabilities,但建议升级到最新版本以获得更好的兼容性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 10:11:15