java - 在 appium android 自动化中找不到 MobileElement

标签 java selenium selenium-webdriver appium

我将使用 appium
自动化 Android 应用程序

配置:
appium版本:1.8.1
Selenium 服务器独立:2.53.0
java客户端:4.1.2

实际上,当我运行脚本时,会出现一些错误,例如

org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement

谁能给出解决方案吗?

我的代码:

public class Sample {

//public static WebDriver driver= null; 



    AndroidDriver driver;

  @Before
    public void setup() throws MalformedURLException {

        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability("deviceName", "HKL3LA2M");
        capabilities.setCapability(CapabilityType.PLATFORM, "Android");
        capabilities.setCapability("platformVersion","8");
        capabilities.setCapability("appPackage", "com.manash.purplle");
        capabilities.setCapability("appActivity", "com.manash.purplle.activity.SplashActivity");
        capabilities.setCapability("noReset", "true");


        // File file=new File("/home/chinna/Downloads", "purplleAndroid-1.8.2.test3.apk");
        //  capabilities.setCapability("app", file.getAbsolutePath());

        driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    }

  @After
  public void tearDown() {

      driver.quit();

  }
  @Test
  public void testMethod() throws InterruptedException {
     /* WebDriverWait wait = new WebDriverWait(driver, 30);
      wait.until(ExpectedConditions.elementToBeClickable(By
              .id("com.android.packageinstaller:id/permission_allow_button")));
      */


        MobileElement smilyIcon=(MobileElement) (new WebDriverWait(driver,60)).until(ExpectedConditions.presenceOfElementLocated(By.id("com.manash.purplle:id/profile_overflow")));
        smilyIcon.click();
        //driver.findElementById("com.manash.purplle:id/profile_overflow").click();

        /*MobileElement smilyIcon= (MobileElement) driver.findElement(By.id("com.manash.purplle:id/profile_overflow"));
        smilyIcon.click();*/
        driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);

        WebElement parentElement=driver.findElement(By.className("android.widget.ListView"));


        List<WebElement> childElements = parentElement.findElements(By.id("com.manash.purplle:id/title"));
        System.out.println("|__________________________|");
        System.out.println("  Smily popup has " + childElements.size() + " links");
        System.out.println("|__________________________|");

        String expected= "Logout";

        String actual=childElements.get(9).getText();

        if(expected.equals(actual)){

            childElements.get(9).click();
            // Logout.logoutButton(driver).click();

            Logout.logoutButtonAlertYes(driver).click();

        }else{

            childElements.get(9).click();

            Login.username(driver).sendKeys("tjagadeesh37@gmail.com");

            Login.password(driver).sendKeys("1234567890");

            Login.logiButton(driver).click();

            System.out.println("Successfully logged in");

        }


  }

}

失败跟踪:

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement
at app_automation.Sample.testMethod(Sample.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

最佳答案

您很可能使用不兼容版本的 appium 服务器(1.8.1 是最新)和客户端(4.1.2 太旧)。

不要显式设置 selenium 库(除非你有充分的理由),appium 已经将其作为依赖项 => 你可能会导致该操作出现问题

更新appium-java-client版本至6.0.0

尝试清理您的代码:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //set implicit wait first
WebDriverWait wait = new WebDriverWait(driver, 60); // now proceed with explicit wait 
WebElement smilyIcon = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("profile_overflow"))); // no need to put package, will be handled automatically; no need in casting as well, but try with latest client library - I had no issues on my side
smilyIcon.click();

关于java - 在 appium android 自动化中找不到 MobileElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50656009/

相关文章:

java - 如何向应用程序添加主题?

python - 如何在 python 中使用 Selenium 关闭浏览器弹出窗口?

angularjs - 如何使用 Protractor 处理旋转器

java - 单击链接 Selenium 后如何驱动浏览器窗口的新实例

java - 使用 xstream 反序列化具有重复条目的 xml

java - 整合多个部署的 Java webapp 配置文件的策略

java - SocketException 连接重置

python - Selenium:_wait_until_connectable 无限期暂停

python Selenium : WebDriverException: Message: chrome not reachable

java - Selenium WebDriver 在 Google 中不会关闭