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

如何配置Winium与WebDriver(含Remote WebDriver)及实现驱动切换

Got it, let's walk through how to get Winium and Selenium working in sync for your cross-app testing scenario—this is a common use case when desktop and web apps need to interact, so here's a step-by-step breakdown:

1. Local Setup: Initialize Both Drivers

First, make sure you've got the required binaries:

  • Winium Desktop Driver (match your Windows version)
  • Selenium WebDriver binary for your target browser (ChromeDriver, GeckoDriver, etc.)

Initialize Winium Desktop Driver

Start the Winium driver first via command line to reserve a port:

start Winium.Desktop.Driver.exe --port 9999

Then initialize the driver in your test code (example in Python):

from selenium import webdriver

winium_capabilities = {
    "app": "C:\\Full\\Path\\To\\YourDesktopApp.exe",  # Or just "notepad.exe" for built-in apps
    "platformName": "Windows",
    "deviceName": "MyWindowsPC",
    "launchDelay": 2000  # Optional: Wait 2s after app launches to avoid race conditions
}

winium_driver = webdriver.Remote(
    command_executor="http://localhost:9999",
    desired_capabilities=winium_capabilities
)

Initialize Selenium WebDriver

For a local Chrome instance:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")  # Start browser maximized

web_driver = webdriver.Chrome(
    executable_path="path/to/chromedriver.exe",
    options=chrome_options
)
2. Switching Between Drivers (Minimize/Regain Focus)

To toggle focus between your desktop app and browser, use these methods:

Minimize the Desktop App

You can use Winium's built-in script execution, or fall back to Windows API if needed:

# Method 1: Winium's native minimize
winium_driver.execute_script("window.minimize();")

# Method 2: Use pywin32 for more control (install via `pip install pywin32`)
import win32gui
import win32con
app_handle = int(winium_driver.find_element_by_xpath("//Window").get_attribute("NativeWindowHandle"))
win32gui.ShowWindow(app_handle, win32con.SW_MINIMIZE)

Minimize the Browser

Selenium has a straightforward method for this:

web_driver.minimize_window()

Regain Focus on Desktop App

Bring the app back to foreground:

# Method 1: Winium's activate command
winium_driver.execute_script("window.activate();")

# Method 2: pywin32 to restore and focus
win32gui.ShowWindow(app_handle, win32con.SW_RESTORE)
win32gui.SetForegroundWindow(app_handle)

Regain Focus on Browser

Ensure the browser is active and ready:

web_driver.maximize_window()
# Double-check we're targeting the correct window
web_driver.switch_to.window(web_driver.current_window_handle)
3. Remote WebDriver Configuration

If you need to run tests on remote machines, here's how to set up both drivers:

Winium on Remote Windows Machine

  1. On the remote Windows box, start the Winium driver with a publicly accessible host:
Winium.Desktop.Driver.exe --port 9999 --host 0.0.0.0
  1. Connect from your test machine:
winium_driver = webdriver.Remote(
    command_executor="http://remote-win-ip:9999",
    desired_capabilities=winium_capabilities
)

Selenium WebDriver via Remote Grid

  1. Set up a Selenium Grid Hub (on a central server):
java -jar selenium-server-4.x.x.jar hub
  1. Register a Windows node with the hub:
java -jar selenium-server-4.x.x.jar node --hub http://hub-ip:4444/grid/register --port 5555
  1. Connect to the grid from your test code:
web_capabilities = {
    "browserName": "chrome",
    "platformName": "Windows",
    "goog:chromeOptions": {"args": ["--start-maximized"]}
}

web_driver = webdriver.Remote(
    command_executor="http://hub-ip:4444/wd/hub",
    desired_capabilities=web_capabilities
)

Key Desired Capabilities Notes

  • Winium Must-Haves:
    • app: Absolute path to your desktop app EXE, or process name (e.g., "mspaint.exe")
    • platformName: Always set to "Windows"
    • launchDelay: Useful if your app takes time to fully launch
  • Selenium Must-Haves:
    • browserName: Target browser (chrome, firefox, edge)
    • platformName: Target OS (Windows, macOS, etc.)
    • Browser-specific options: Use goog:chromeOptions for Chrome, moz:firefoxOptions for Firefox
4. Pro Tips for Smooth Testing
  • Explicit Waits: Always add waits after switching drivers—apps/browsers need time to reload elements:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Wait for a Winium button to be clickable
winium_wait = WebDriverWait(winium_driver, 10)
winium_wait.until(EC.element_to_be_clickable((By.XPATH, "//Button[@Name='Submit']")))

# Wait for a web element to load
web_wait = WebDriverWait(web_driver, 10)
web_wait.until(EC.presence_of_element_located((By.ID, "submit-form")))
  • Clean Up: Always quit both drivers at the end of tests to avoid lingering processes:
winium_driver.quit()
web_driver.quit()
  • Port Conflicts: Ensure Winium (default 9999) and Selenium Grid (default 4444) use different ports if running on the same machine.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:26:22