在iOS设备Web自动化中向字段发送文本后,按钮未被启用

kninwzqo  于 2023-06-25  发布在  iOS
关注(0)|答案(1)|浏览(79)

我正在使用Java客户端的appium自动化Web应用程序。在我的自动化中,我需要在字段上写入文本,启用“继续”按钮并按下它。
首先,我尝试使用

textField.sendKeys("text to send");

但它失败了,因为在iOS设备上,“继续”按钮没有被启用,我想必要的事件没有被触发来启用按钮。
经过一些研究,我最终使用了Selenium 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();
        }
    }

知道为什么会这样吗这是我第一次直接使用selenium API,我通常使用appium java库来自动化本地应用程序。

i86rm4rw

i86rm4rw1#

最后,在我的例子中,按钮具有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));
    }

相关问题