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

在Windows平台使用Appium+JavaScript时无法通过By.name定位器点击元素的解决方案咨询

Hey there! I get it—you're working on Windows desktop app automation with Appium and JavaScript, and hitting a wall with By.name() even though it works smoothly in Python. Let's figure out what's going wrong and get this fixed.

What's Causing the Error?

The error message css selector locator strategy is not supported gives away the issue: in the JavaScript selenium-webdriver library, the By.name() method automatically converts your request to a CSS selector (like [name="File"]). But the Windows Appium Driver doesn't support CSS selectors as a valid locator strategy. Meanwhile, the Python Appium client directly maps find_element_by_name() to the Windows Driver's native name locator strategy, which is why it works there.

Fixes to Try

We have two solid solutions to bypass this conversion and use the name attribute correctly:

Solution 1: Explicitly Use the name Locator Strategy

Instead of relying on By.name(), directly tell the driver to use the name strategy with a raw locator object. This skips the CSS selector conversion entirely:

"use strict"; 
var wd = require("selenium-webdriver"), By = wd.By, until = wd.until; 

// Fix: Escape backslashes in the app path (Windows requires double backslashes)
var desiredCaps = { 
    platformName: "Windows", 
    deviceName: "windowsPC", 
    app: "C:\\Program Files\\xxx\\bin\\xxx.exe", 
    browserName: "" 
}; 

async function testEribank() { 
    let driver;
    try {
        driver = await new wd.Builder() 
            .usingServer("http://127.0.0.1:4723") 
            .withCapabilities(desiredCaps) 
            .build(); 

        // Explicitly use the 'name' locator strategy
        const fileElement = await driver.findElement({ using: 'name', value: 'File' }); 
        await fileElement.click(); // Add await—click is an async operation!
        console.log("Successfully clicked the 'File' element");
    } catch (error) {
        console.error("Test failed:", error);
    } finally {
        // Make sure to clean up the driver even if something goes wrong
        if (driver) await driver.quit();
    }
} 

testEribank();

Solution 2: Use XPath to Target the name Attribute

The Windows Driver supports XPath, so you can directly match elements by their name attribute using an XPath query:

// Replace the element locator line with this
const fileElement = await driver.findElement(By.xpath('//*[@name="File"]')); 
await fileElement.click();

Quick Extra Tips

  • Fix Path Escaping: Your original app path uses single backslashes, which will be interpreted as escape characters in JavaScript. Always use double backslashes (\\) or forward slashes (/) for Windows paths.
  • Await All Async Operations: Methods like findElement, click, and quit are asynchronous—don't forget to add await to avoid unhandled promise warnings.
  • Error Handling: Wrapping your code in try/catch/finally ensures you catch errors and clean up resources properly, which prevents lingering driver instances.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 18:42:43