我在特定 EditText
字段中执行发送键时遇到问题。我尝试了一个脏的 xpath 和 id,但它仍然没有进入文本字段。但是我觉得找到元素没有问题。但是它不会点击元素。
所以在应用程序中发生的事情是,当用户选择页面时,它会打开一个 WebView ,并且 EditText
字段位于该 WebView
中。
这是我从 appium 检查员那里得到的脏 xpath:
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.support.v4.view.ViewPager/android.view.ViewGroup/android.widget.EditText[1]
这是漂亮的 id:
@FindBy(id = "test_number")
public WebElement testNumberTextField;
我也尝试使用脏的 xpath 通过 xpath 查找。
这是我尝试输入文本的方法。您可以通过注释代码看到我也尝试了其他方法来使其工作但仍然没有成功:
public void enterTestNumber(){
//List<WebElement> menu22 =drive.getDriver().findElements(By.className("android.widget.EditText"));
// menu22.get(1).sendKeys(randomStringGeneratorMethods.randomAlphaNumericString(7));
testPage.testNumberTextField.sendKeys(randomStringGeneratorMethods.randomAlphaNumericString(7));
//js.executeScript("document.getElementById('"+testPage.testNumberTextField+").value='"+randomStringGeneratorMethods.randomAlphaNumericString(7)+"';");
}
随机生成器只是一个随机字符串生成器,它是这样的:
public String randomAlphaNumericString(int characterLength) {
return RandomStringUtils.randomAlphanumeric(characterLength);
}
有谁知道如何让它工作以便它可以在这个 EditText
字段中键入文本?
最佳答案
由于元素在 webview 中,您需要使用 chrome/firefox 找到元素 html,因为 android ui automator 无法检测到该元素。
要获取元素的html,您需要将您的设备与chrome连接并转到chrome://inspect
,现在在您的应用程序中运行并打开webview,您会发现您的设备打开 webview 后,在 chrome://inspect
链接中,您需要从那里选择您的设备,您会看到 webview 的副本,从那里您可以检查元素并找到元素的 html 并使用该 html 您可以获得 xpath。
现在您已经获得了 xpath,现在您只需要将驱动程序从您的代码切换到 webview,然后您就可以使用 sendKeys()
方法在字段中输入值。
关于java - 无法在 EditText 字段中发送键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082847/