Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中为没有ID的Android元素创建Xpath_Java_Android_Selenium_Appium_Genymotion - Fatal编程技术网

在java中为没有ID的Android元素创建Xpath

在java中为没有ID的Android元素创建Xpath,java,android,selenium,appium,genymotion,Java,Android,Selenium,Appium,Genymotion,我一直在寻找这个问题有一段时间了,所以我决定创建一个帐户,自己问这个问题。这是我的第一篇文章,所以我会尽我所能 我想在某个屏幕中填充三个文本字段。这些字段没有id、索引或content-desc。所有字段都具有相同的类和包。他们仅有的细节是文字 我用的是Genymotion、Appium和Selenium 这是我的代码: package Steps; import cucumber.api.PendingException; import cucumber.api.java.After; imp

我一直在寻找这个问题有一段时间了,所以我决定创建一个帐户,自己问这个问题。这是我的第一篇文章,所以我会尽我所能

我想在某个屏幕中填充三个文本字段。这些字段没有id、索引或content-desc。所有字段都具有相同的类和包。他们仅有的细节是文字

我用的是Genymotion、Appium和Selenium

这是我的代码:

package Steps;
import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;

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


public class MyStepdefs {

    AndroidDriver driver;

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "192.168.150.101:5555");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.android.contacts");
        capabilities.setCapability("appActivity", "com.android.contacts.activities.PeopleActivity");
        driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
因此,我想在虚拟设备中添加一个新联系人。我能够打开正确的屏幕添加新联系人,但现在我需要填写一些文本字段,这就是我的问题开始的地方

我想对以下字段使用sendKeys方法:

我尝试了以下Xpath:

MobileElement nameField = (MobileElement) driver.findElement(By.xpath("//android.widget.EditText[@text='Name']"));
    nameField.click();
    nameField.sendKeys("Test");
我的应用程序日志:

    [HTTP] {"using":"xpath","value":"//android.widget.EditText[@text='Name']"}
[W3C] Calling AppiumDriver.findElement() with args: ["xpath","//android.widget.EditText[@text='Name']","e8c8c5fc-0dbc-42b5-835d-f27b0a365a95"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 15000 ms for condition
[AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.EditText[@text='Name']","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.EditText[@text='Name']","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.widget.EditText[@text='Name']' using 'XPATH' with the contextId: '' multiple: false
[HTTP] <-- POST /wd/hub/session/e8c8c5fc-0dbc-42b5-835d-f27b0a365a95/element - - ms - -
[HTTP] 
[HTTP] --> DELETE /wd/hub/session/e8c8c5fc-0dbc-42b5-835d-f27b0a365a95
[HTTP] {}
[W3C] Calling AppiumDriver.deleteSession() with args: ["e8c8c5fc-0dbc-42b5-835d-f27b0a365a95"]
[BaseDriver] Event 'quitSessionRequested' logged at 1530018954452 (15:15:54 GMT+0200 (W. Europe Daylight Time))
[Appium] Removing session e8c8c5fc-0dbc-42b5-835d-f27b0a365a95 from our master session list
[AndroidDriver] Shutting down Android driver
[ADB] Running 'D:\Handleiding\SDK\platform-tools\adb.exe -P 5037 -s 192.168.150.101\:5555 shell am force-stop com.android.contacts'
[ADB] Pressing the HOME button
[ADB] Running 'D:\Handleiding\SDK\platform-tools\adb.exe -P 5037 -s 192.168.150.101\:5555 shell input keyevent 3'
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.widget.EditText[@text='Name']' using 'XPATH' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"No element found"}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Matched JSONWP error code 7 to NoSuchElementError
[AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN
[AndroidBootstrap] Received command result from bootstrap
[UiAutomator] Shutting down UiAutomator
[UiAutomator] Moving to state 'stopping'
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Closed client connection
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: numtests=1
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=.
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
[UiAutomator] UiAutomator shut down normally
[UiAutomator] Moving to state 'stopped'
[ADB] Attempting to kill all uiautomator processes
[ADB] Getting all processes with uiautomator
[ADB] Running 'D:\Handleiding\SDK\platform-tools\adb.exe -P 5037 -s 192.168.150.101\:5555 shell ps'
[ADB] No uiautomator process found to kill, continuing...
[UiAutomator] Moving to state 'stopped'
[Logcat] Stopping logcat capture
[ADB] Running 'D:\Handleiding\SDK\platform-tools\adb.exe -P 5037 -s 192.168.150.101\:5555 shell am force-stop io.appium.unlock'
[AndroidDriver] Not cleaning generated files. Add `clearSystemFiles` capability if wanted.
[BaseDriver] Event 'quitSessionFinished' logged at 1530018960995 (15:16:00 GMT+0200 (W. Europe Daylight Time))
[W3C] Received response: null
[W3C] But deleting session, so not returning
[W3C] Responding to client with driver.deleteSession() result: null
[HTTP] <-- DELETE /wd/hub/session/e8c8c5fc-0dbc-42b5-835d-f27b0a365a95 200 6545 ms - 14
[HTTP] 
我甚至尝试了一个列表,同样的结果:

driver.findElement(By.xpath("//android.widget.EditText[@text='Name']")).click();
driver.findElement(By.xpath("//android.widget.EditText[@text='Name']")).sendKeys("Test");
List<AndroidElement> txtbox = driver.findElements(By.className("android.widget.EditText"));
txtbox.get(0).sendKeys("Test");
txtbox.get(1).sendKeys("0612345678");
txtbox.get(2).sendKeys("test@testtest.test");
List txtbox=driver.findElements(By.className(“android.widget.EditText”);
获取(0).sendKeys(“测试”);
获取(1)发送键(“0612345678”);
txtbox.get(2.sendKeys)(“test@testtest.test");
如果你对我的帖子有任何建议,请让我知道,这样我就可以编辑它了


提前谢谢

尝试在声明和实例化中将webdriver定义为AndroidDriver

同时将MobileElement更改为AndroidElement


您的xpath在我看来是正确的。

尝试在声明和实例化中将您的webdriver定义为AndroidDriver。同时将MobileElement更改为AndroidElement。你的xpath在我看来是正确的。我能想到的另一件事是,当该页面未加载时,您正在尝试访问该元素。@BillHileman感谢您的快速回复。我尝试了你的建议,但得到了同样的结果。我还添加了5秒钟的睡眠,只是为了检查它是否有效,但也没有运气。你确定它会进入那个特定的屏幕吗?您还可以尝试
AndroidElement txtbox=driver.findElement(By.xpath(“//android.widget.EditText[contains(text(),'Name')”)))以防文本字段中可能有隐藏字符。我已经在emulator上查看了contacts应用程序。它几乎相同,可能是一个更新的API,它有名字和姓氏,不仅仅是名字,在其他方面基本相同。你是怎么进入那一页的?您能否显示在初始化驱动程序和尝试访问名称字段之间所执行的步骤?@Innoh您可以在工作中使用文本框的边界值。