iOS设备网页自动化发送文本到字段后按钮未启用

huangapple go评论68阅读模式
英文:

Button not getting enabled after sending text to field in iOS devices web automation

问题

I am automating a web app using appium with Java client. In my automation, I need to write text on a field, enabling the 'continue' button and press it.

Firstly, I tried using

textField.sendKeys("text to send");

but it failed because, on iOS devices the 'continue' button was not getting enabled, I suppose the necessary events were not getting triggered to enable the button.

After some research, I ended up using Selenium Actions API. I created the following function

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();
}

The problem is that this function always throws StaleElementReferenceException even though I find the element every time I call this function.

I even added a wait beforehand ignoring StaleElementReferenceException.

Also, I tried to catch it and find the element again but same problem.

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();
        }
    }

Any idea why this is happening? This is the first time I use selenium api directly, I usually automate native applications using appium java library instead.

英文:

I am automating a web app using appium with Java client. In my automation, I need to write text on a field, enabling the 'continue' button and press it.

Firstly, I tried using

textField.sendKeys("text to send");

but it failed because, on iOS devices the 'continue' button was not getting enabled, I suppose the necessary events were not getting triggered to enable the button.

After some research, I ended up using Selenium Actions API. I created the following function

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();
}

The problem is that this function always throws StaleElementReferenceException even though I find the element every time I call this function.

I even added a wait beforehand ignoring StaleElementReferenceException.

Also, I tried to catch it and find the element again but same problem.

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();
        }
    }

Any idea why this is happening? This is the first time I use selenium api directly, I usually automate native applications using appium java library instead.

答案1

得分: 0

最终,在我的情况下,按钮具有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));
}
英文:

Ultimately, in my case, the button had the disabled attribute, which was getting removed when the textfield was not empty.

So, what I did was to remove this property using javascriptExecutor, which lets me execute javascript code.

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

huangapple
  • 本文由 发表于 2023年6月15日 18:07:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定