Appium getText()在AndroidElement上的sendKeys()之后抛出NoSuchElementException

问题描述 投票:0回答:1

我有android:hint="oldtext"的EditText。在Appium测试项目中,通过Android UIAutomator找到这个元素:

WebElement element = 
  driver.findElementByAndroidUIAutomator("new UiSelector().text(\"oldtext\");

然后我将新文本发送到元素并调用get方法

element.sendKeys("newText");
element.getText();

我想断言新文本,但它抛出NoSuchElementException

org.openqa.selenium.NoSuchElementException: UiSelector[TEXT=oldText]
For documentation on this error, please visit:     
http://seleniumhq.org/exceptions/no_such_element.html
android appium appium-android
1个回答
1
投票

对于appium,您可以使用id,resource-id,cont-desc或xpath来唯一标识元素。如果您在app元素中没有看到任何id或automationId(cont-desc),那么请您的开发人员将它们放入代码中。

建议不要使用xpath,但如果没有任何id或cont-desc,则可以使用。

pic from http://www.automationtestinghub.com

现在您可以访问您的元素,如下所示。

WebElement element= driver.findElement(By.id("element Id")).sendKeys("new Text");
//or
WebElement element = driver.findElementById("element id").sendKeys("new Text");

//using accessibility id
WebElement element = driver.findElementsByAccessibilityId("accesibility Id");

//using xpath
WebElement element = driver.findElement(By.xpath("//EditText[contains(@text,'oldtext')]"));
© www.soinside.com 2019 - 2024. All rights reserved.