Не удается найти элемент в Chrome с помощью Appium Java - PullRequest
0 голосов
/ 30 апреля 2018

Я изучаю appium и пытаюсь выполнить основную операцию поиска в Google в Appium Java. Код, который я написал:

package com.MavenTest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class StartChrome {

    @Test
    public void test1() throws MalformedURLException, InterruptedException {

        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", "My Phone");
        caps.setCapability("udid", "790dc03c"); // Give Device ID of your mobile phone
        caps.setCapability("platformName", "Android");
        caps.setCapability("platformVersion", "5.1.1");
        caps.setCapability("browserName", "Chrome");
        caps.setCapability("noReset", true);

        // Create object of AndroidDriver class and pass the url and capability that we

        // System.setProperty("webdriver.chrome.driver",
        // "D:\\workspace\\AppiumTest\\driver\\chromedriver.exe");

        AppiumDriver<MobileElement> driver = null;
        try {
            driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        }

        // Open URL in Chrome Browser
        driver.get("http://www.google.com");

        System.out.println("Title " + driver.getTitle());

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.name("Search")));

        driver.findElementByName("q").sendKeys("google");
        Thread.sleep(2000);
        driver.findElementByName("Gogle Search").click();
        driver.quit();

    }

}

ошибка, которую я получаю при попытке отправить ключ:

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement не может быть приведен к io.appium.java_client.MobileElement

Ответы [ 2 ]

0 голосов
/ 18 сентября 2018

Я столкнулся с той же проблемой, обновление библиотеки клиента Appium исправляет ее.

<!-- https://mvnrepository.com/artifact/io.appium/java-client -->

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>5.0.4</version>
</dependency>
0 голосов
/ 30 апреля 2018

MobileElement является производным от RemoteWebElement.

Если вы напишите такой код:

driver.findElementByName("q").sendKeys("google");

Вы пытаетесь привести RemoteWebElement к одному из его подклассов MobileElement.

java.lang.ClassCastException Проблема возникает, если вы напрямую обращаетесь к методу, как показано ниже:

driver.findElementByName("q").sendKeys("google");

WorkAround: Вы должны привести к MobileElement, как показано ниже:

MobileElement find = (MobileElement) driver.findElementByName("q").sendKeys("google");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...