英文:
Java Selenium delete default input field value and add own values
问题
我目前不得不涉足Selenium,因为有一个项目需要。我应该自动加载一个页面,登录,然后点击并填写一个表格。
一切都正常,直到我填写表格。
我的代码:
public void inputGross(String gross) {
inputGross.sendKeys(Keys.DELETE, gross);
}
问题是输入字段已经包含金额“0.00 €”。
使用Clear命令清空字段,但由于首先离开单元格然后再次进入,会再次创建默认值“0.00 €”。
我已经尝试过的:
inputGross.clear()
inputGross.sendKeys(gross)
inputGriss.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE, gross));
inputGriss.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE, gross, Keys.RETURN));
inputGriss.sendKeys(Keys.DELETE, Keys.DELETE, Keys.DELETE, Keys.DELETE, gross, Keys.RETURN));
我已经没有更多的想法。
我还尝试使用Keys.ARROW_LEFT
向左移动,然后删除所有内容。但这也不起作用。
我真的需要帮助。
英文:
I'm currently having to venture into Selenium due to a project.
I'm supposed to load a page automatically, log in, click and then fill in a table.
Everything works until I fill out the table.
My Code:
public void inputGross(String gross){
inputGross.sendKeys(Keys.DELETE, gros);
The problem is that the input field already contains the amount "0.00 €"
.
With the Clear command, the field empties, but since it first leaves the cell and then enters again, the default value "0.00 €"
is created again.
What I have already tried:
inputGross.clear()
inputGross.sendKeys(gross)
inputGriss.sendKeys(Keys.chord(Keyes.CONTROLL, "a"), Keys.DELETE, gross));
inputGriss.sendKeys(Keys.chord(Keys.CONTROLL, "a"), Keys.DELETE, gross, Keys.RETURN));
inputGriss.sendKeys(Keys.DELETE, Keys.DELETE, Keys.DELETE, Keys.DELETE, gross, Keys.RETURN));
I have no more ideas.
I also tried using Keys.ARROW_LEFT
to go to the left side and then delete everything. But that doesn't work either.
I really need help
答案1
得分: 1
尝试使用操作。
此代码应解决您的问题:
public void inputGross(String gross){
Actions actions = new Actions(driver);
inputGross.click();
for (char ch: gross.toCharArray()) {
actions.sendKeys(Keys.BACK_SPACE).perform();
}
actions.sendKeys(inputGross, gross).perform();
}
英文:
Try to use Actions.
This code should solve your problem:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
public void inputGross(String gross){
Actions actions = new Actions(driver);
inputGross.click();
for (char ch: gross.toCharArray()) {
actions.sendKeys(Keys.BACK_SPACE).perform();
}
actions.sendKeys(inputGross, gross).perform();
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论