无法使用appium java在chrome中定位元素

无法使用appium java在chrome中定位元素,java,appium,Java,Appium,我正在学习appium,并尝试在appium java中执行一个基本的google搜索操作。我写的代码是: 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.

我正在学习appium,并尝试在appium java中执行一个基本的google搜索操作。我写的代码是:

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();

    }

}
package com.MavenTest;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.testng.annotations.Test;
导入io.appium.java_client.AppiumDriver;
导入io.appium.java_client.MobileElement;
导入io.appium.java_client.android.AndroidDriver;
公共级StartChrome{
@试验
public void test1()引发畸形的DurException、InterruptedException{
DesiredCapabilities=新DesiredCapabilities();
caps.setCapability(“deviceName”、“我的手机”);
caps.setCapability(“udid”,“790dc03c”);//给出您手机的设备ID
caps.setCapability(“平台名”、“安卓”);
caps.setCapability(“平台版”、“5.1.1”);
caps.setCapability(“浏览器名”、“浏览器名”);
caps.setCapability(“noReset”,真);
//创建AndroidDriver类的对象,并传递我们需要的url和功能
//System.setProperty(“webdriver.chrome.driver”,
//“D:\\workspace\\AppiumTest\\driver\\chromedriver.exe”);
APPIUMDRIVE驱动程序=null;
试一试{
驱动程序=新的AndroidDriver(新的URL(“http://0.0.0.0:4723/wd/hub"(大写),;
}捕获(格式错误){
System.out.println(e.getMessage());
}
//在Chrome浏览器中打开URL
驱动程序。获取(“http://www.google.com");
System.out.println(“Title”+driver.getTitle());
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
wait.until(通过(按.name(“搜索”))查找的所有事件的预期条件、可见性);
driver.findElementByName(“q”).sendKeys(“谷歌”);
《睡眠》(2000年);
driver.findElementByName(“Gogle搜索”)。单击();
driver.quit();
}
}
尝试发送密钥时出现的错误是:

java.lang.ClassCastException: 无法将org.openqa.selenium.remote.RemoteWebElement强制转换为 io.appium.java_client.MobileElement


MobileElement派生自RemoteWebElement

如果您编写这样的代码:

driver.findElementByName("q").sendKeys("google");
您正试图将RemoteWebElement强制转换为其子类MobileElement之一

如果您直接访问如下方法,则会出现java.lang.ClassCastException问题:

driver.findElementByName("q").sendKeys("google");
MobileElement find = (MobileElement) driver.findElementByName("q").sendKeys("google");
解决方法:您必须强制转换到MobileElement,如下所示:

driver.findElementByName("q").sendKeys("google");
MobileElement find = (MobileElement) driver.findElementByName("q").sendKeys("google");

我也面临同样的问题,升级Appium客户端库修复了它

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

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>5.0.4</version>
</dependency>

木卫一
java客户端
5.0.4

是否有任何解决方法,使我不必每次都将其装箱到MobileElement?尝试了此解决方法,但问题仍然存在。我建议您显式地将类型转换为MobileElement,它已经在尝试通过隐式类型转换来实现。因此,问题依然存在。上面的代码是否正确,我实例化的驱动程序对象是否正确?让我提供更多信息,我在pom.xml中使用的依赖项如下:io.appium java client 5.0.2 org.testng testng 6.8 test