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

@iOSFindBy已废弃,混合项目替代方案及注解报错咨询

Fixing the @iOSXCUITFindBy Annotation Error in Appium PageFactory

Hey there! I’ve run into this exact issue when migrating Appium tests from Android to iOS, so let me walk you through the fixes step by step.

First, why was @iOSFindBy deprecated?

Appium phased out the old UIAutomation driver for iOS in favor of the more stable XCUITest driver. The @iOSFindBy annotation was tied to the deprecated UIAutomation driver, which is why you’re seeing the deprecation warning. The replacement @iOSXCUITFindBy is designed specifically for the XCUITest driver—the standard for modern iOS testing.

Why are you getting "Annotation are not allowed here"?

This error almost always comes down to one of three issues—let’s check each:

  1. Wrong import package
    Make sure you’re importing the correct @iOSXCUITFindBy annotation from the Appium Java Client library. The correct import is:

    import io.appium.java_client.pagefactory.iOSXCUITFindBy;
    

    Avoid importing any similarly named annotations from old Appium legacy packages or custom code.

  2. Incorrect annotation parameter name
    You used Accessibility = "button_login" with the old @iOSFindBy, but @iOSXCUITFindBy uses a different parameter name: accessibilityId (note the lowercase 'a' and camelCase). This is a super common gotcha!

  3. PageFactory initialization mismatch
    When initializing your Page Object, you need to use an IOSDriver (not AndroidDriver) with the AppiumFieldDecorator. If you accidentally use an Android driver to initialize an iOS page, the compiler might throw annotation-related errors.

Corrected Code Example

Here’s how your element definition should look after fixing the above issues:

// Correct imports at the top of your Page Object class
import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;

public class LoginPage {
    // Fixed annotation order and parameter name
    @AndroidFindBy(id = "net.myApp.int:id/loginBtn")
    @iOSXCUITFindBy(accessibilityId = "button_login")
    private MobileElement login_Button2;

    // Rest of your page object methods...
}

And here’s how to properly initialize the page with an iOS driver:

// In your test class
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;

// Set up your iOS desired capabilities first
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "iOS");
caps.setCapability("deviceName", "iPhone 14");
caps.setCapability("app", "/path/to/your/ios/app.ipa");
// Add other necessary capabilities...

IOSDriver<MobileElement> iosDriver = new IOSDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
LoginPage loginPage = PageFactory.initElements(new AppiumFieldDecorator(iosDriver), LoginPage.class);

One Last Check

Ensure your Appium Java Client dependency is up to date (version 7.0.0 or newer). Older versions might not include the @iOSXCUITFindBy annotation at all, which would also cause errors. For example, if you’re using Maven, your dependency should look something like:

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>8.5.1</version> <!-- Use the latest stable version -->
</dependency>

That should resolve both the deprecation warning and the annotation error. Let me know if you hit any other snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:57:23