[英]Not able to locate element in chrome by using appium java
我正在學習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();
}
}
嘗試發送密鑰時遇到的錯誤是:
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:
MobileElement find = (MobileElement) driver.findElementByName("q").sendKeys("google");
我遇到了同樣的問題,升級Appium Client庫可以解決該問題。
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.4</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.