英文:
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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论