【问题标题】:MobileElement was not found in appium android automation在 appium android 自动化中找不到 MobileElement
【发布时间】:2018-06-03 19:22:06
【问题描述】:

我将使用 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)

【问题讨论】:

  • 您的应用程序在设备上启动并运行了吗?
  • 是的..它正在打开并突然关闭。
  • 我猜它无法检测到该元素。但无法解决。你遇到过这个问题吗?
  • 谢谢,你可以尝试使用像 driver.findElementById("profile_overflow").click(); 这样的元素 ID,因为你必须使用 testNG 框架。
  • 您可以应用以下更改:- 将 Java 客户端更改为至少 5.01 或 5.0.4。

标签: java selenium selenium-webdriver appium


【解决方案1】:

您很可能使用了不兼容的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2017-12-20
    • 2018-06-28
    • 2020-06-23
    • 1970-01-01
    • 2014-01-31
    • 2016-01-26
    相关资源
    最近更新 更多