我正在使用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库来自动化原生应用程序.

推荐答案

最终,在我的例子中,按钮具有disabled属性,当文本字段不为空时,该属性将被删除.

因此,我所做的是使用javascriptExecutor删除该属性,它允许我执行javascript代码.

public void enterTextField(String txt) throws InterruptedException {
    getDriver().findElement(textField).sendKeys("My Text");
    
    ((JavascriptExecutor) getDriver()).executeScript("arguments[0].removeAttribute('disabled');",
                getDriver().findElement(continueButton));
    }

Ios相关问答推荐

第三方框架的iOS隐私声明

在Swift5中,将某些属性值从一个类复制到另一个类实例

UIAlertController的空子类是否可以与`appearance(whenContainedInInstancesOf:)`一起安全使用

swift|在具有自定义单元格的多个TableView中没有任何内容

从包含 5 个以上项目的自定义选项卡栏的导航视图中删除更多按钮

Swift Combine和iOS版本限制?

SwiftUI HStack 文本字段对齐

由于缺少 libarclite_iphonesimulator.a 文件,将 XCode 升级到 14.3 后无法在模拟器上运行 Flutter 应用程序

@MainActor 类的扩展是主要演员吗?

如何在 Mac OS Ventura 中使用 Xcode 13

使用 SceneKit 从 CapturedRoom.walls 重新创建 RoomPlan

.onAppear() 函数不适用于 NavigationLink

在 UICollectionView 上动态设置布局导致莫名其妙的 contentOffset 变化

如何在 SwiftUI 中检测 TextField 的实时变化?

ios 崩溃 EXC_BAD_ACCESS KERN_INVALID_ADDRESS

Xcode 4.1 致命错误:自构建预编译头文件后修改了 stdlib

Xcode 11 向后兼容性:UIWindowScene 仅在 iOS 13 或更高版本中可用

在 iOS 8.1 模拟器上更改语言不起作用

使用 Swift 在一个 ViewController 中强制横向模式

未找到符号:kUTTypeImage