我正在使用Java客户端的Appium自动化一个网络应用程序.在我的自动化系统中,我需要在字段中写入文本,启用"继续"按钮并按下它.
首先,我试着用
textField.sendKeys("text to send");
但它失败了,因为在iOS设备上,‘Continue’按钮没有被启用,我想必要的事件没有被触发来启用该按钮.
经过一些研究,我最终使用了Selify Actions API.我创建了以下函数
public void enterTextField(String txt) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(20));
wait.ignoring(StaleElementReferenceException.class)
.until(ExpectedConditions.visibilityOfElementLocated(by));
new Actions(getDriver())
.sendKeys(getDriver().findElement(textField), txt)
.perform();
}
问题是,此函数总是抛出StaleElementReferenceException,即使我每次调用此函数时都会找到元素.
我甚至在忽略StaleElementReferenceException之前添加了等待.
此外,我试图抓住它,并再次找到元素,但同样的问题.
public void enterTextField(String txt) {
WebDriverWait wait = new WebDriverWait(getDriver(),
Duration.ofSeconds(20));
wait.ignoring(StaleElementReferenceException.class)
.until(ExpectedConditions.visibilityOfElementLocated(by));
try {
new Actions(getDriver())
.sendKeys(getDriver().findElement(textField), txt)
.perform();
} catch (StaleElementReferenceException e) {
TestUtils.log().warn("Send keys to text field failed, retrying... {}", e.toString());
new Actions(getDriver())
.sendKeys(getDriver().findElement(textField), txt)
.perform();
}
}
你知道为什么会发生这种事吗?这是我第一次直接使用Selify API,我通常会使用Appium Java库来自动化原生应用程序.