Selenium WebDriver:我想覆盖字段中的值,而不是使用 Java 通过 sendKeys 附加到它

发布于 2024-09-10 12:56:12 字数 216 浏览 7 评论 0原文

在 WebDriver 中,如果我使用 sendKeys,它会将我的字符串附加到字段中已存在的值中。我无法使用clear()方法清除它,因为我第二次这样做时,网页会抛出一个错误,说它必须在10到100之间。所以我无法清除它,否则会在之前抛出错误我可以使用 sendKeys 输入新值,如果我 sendKeys 它只是将其附加到已有的值中。

WebDriver 中是否有任何内容可以让您覆盖字段中的值?

In WebDriver, if I use sendKeys it will append my string to the value that already exists in the field. I can't clear it by using clear() method because the second I do that, the webpage will throw an error saying that it has to be between 10 and 100. So I can't clear it or an error will be thrown before I can put in the new value using sendKeys, and if I sendKeys it just appends it to the value already there.

Is there anything in WebDriver that lets you overwrite the value in the field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

白色秋天 2024-09-17 12:56:12

您还可以在发送密钥之前清除该字段。

element.clear()
element.sendKeys("Some text here")

You can also clear the field before sending it keys.

element.clear()
element.sendKeys("Some text here")
箜明 2024-09-17 12:56:12

我认为您可以尝试首先选择字段中的所有文本,然后发送新序列:

from selenium.webdriver.common.keys import Keys
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");

I think you can try to firstly select all the text in the field and then send the new sequence:

from selenium.webdriver.common.keys import Keys
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
自演自醉 2024-09-17 12:56:12

好吧,那是前几天的事了……
在我目前的情况下,ZloiAdun 的答案对我不起作用,但让我非常接近我的解决方案......

而不是:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");

以下代码让我很高兴:

element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55");

所以我希望这对某人有帮助!

Okay, it is a few days ago...
In my current case, the answer from ZloiAdun does not work for me, but brings me very close to my solution...

Instead of:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");

the following code makes me happy:

element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55");

So I hope that helps somebody!

萌酱 2024-09-17 12:56:12

如果它对任何人有帮助,ZloiAdun 的答案的 C# 等效项是:

element.SendKeys(Keys.Control + "a");
element.SendKeys("55");

In case it helps anyone, the C# equivalent of ZloiAdun's answer is:

element.SendKeys(Keys.Control + "a");
element.SendKeys("55");
鹿港巷口少年归 2024-09-17 12:56:12

这对我有用。

mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE);

This worked for me.

mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE);
墨小墨 2024-09-17 12:56:12

使用这个,它是值得信赖的解决方案,并且适用于所有浏览器:

protected void clearInput(WebElement webElement) {
    // isIE() - just checks is it IE or not - use your own implementation
    if (isIE() && "file".equals(webElement.getAttribute("type"))) {
        // workaround
        // if IE and input's type is file - do not try to clear it.
        // If you send:
        // - empty string - it will find file by empty path
        // - backspace char - it will process like a non-visible char
        // In both cases it will throw a bug.
        // 
        // Just replace it with new value when it is need to.
    } else {
        // if you have no StringUtils in project, check value still empty yet
        while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
            // "\u0008" - is backspace char
            webElement.sendKeys("\u0008");
        }
    }
}

如果输入有 type="file" - 不要在 IE 中清除它。它将尝试通过空路径查找文件并抛出错误。

您可以在我的博客上找到更多详细信息

Use this one, it is trusted solution and works well for all browsers:

protected void clearInput(WebElement webElement) {
    // isIE() - just checks is it IE or not - use your own implementation
    if (isIE() && "file".equals(webElement.getAttribute("type"))) {
        // workaround
        // if IE and input's type is file - do not try to clear it.
        // If you send:
        // - empty string - it will find file by empty path
        // - backspace char - it will process like a non-visible char
        // In both cases it will throw a bug.
        // 
        // Just replace it with new value when it is need to.
    } else {
        // if you have no StringUtils in project, check value still empty yet
        while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
            // "\u0008" - is backspace char
            webElement.sendKeys("\u0008");
        }
    }
}

If input has type="file" - do not clear it for IE. It will try to find file by empty path and will throw a bug.

More details you could find on my blog

权谋诡计 2024-09-17 12:56:12

使用大多数提到的方法时遇到问题,因为文本字段不接受键盘输入,并且鼠标解决方案似乎不完整。

这可以模拟在字段中单击,选择内容并将其替换为新内容。

     Actions actionList = new Actions(driver);
     actionList.clickAndHold(WebElement).sendKeys(newTextFieldString).
     release().build().perform();

Had issues using most of the mentioned methods since textfield had not accepted keyboard input, and the mouse solution seem not complete.

This worked for to simulate a click in the field, selecting the content and replacing it with new.

     Actions actionList = new Actions(driver);
     actionList.clickAndHold(WebElement).sendKeys(newTextFieldString).
     release().build().perform();
瀟灑尐姊 2024-09-17 12:56:12

使用以下内容:

driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value");

Use the following:

driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value");
凤舞天涯 2024-09-17 12:56:12

原来的问题说不能使用clear()。这不适用于那种情况。我在这里添加我的工作示例,因为这篇 SO 帖子是在输入值之前清除输入的第一个 Google 结果之一。

对于没有额外限制的输入,我使用 NodeJS 为 Selenium 提供了一个与浏览器无关的方法。此代码片段是我在测试脚本中使用 var test = require( 'common' ); 导入的公共库的一部分。它用于标准节点 module.exports 定义。

when_id_exists_type : function( id, value ) {
driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 )
    .then( function() {
        var el = driver.findElement( webdriver.By.id( id ) );
        el.click();
        el.clear();
        el.sendKeys( value );
    });
},

找到该元素,单击它,清除它,然后发送密钥。

此页面有完整的代码示例以及可能有帮助的文章

The original question says clear() cannot be used. This does not apply to that situation. I'm adding my working example here as this SO post was one of the first Google results for clearing an input before entering a value.

For input where here is no additional restriction I'm including a browser agnostic method for Selenium using NodeJS. This snippet is part of a common library I import with var test = require( 'common' ); in my test scripts. It is for a standard node module.exports definition.

when_id_exists_type : function( id, value ) {
driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 )
    .then( function() {
        var el = driver.findElement( webdriver.By.id( id ) );
        el.click();
        el.clear();
        el.sendKeys( value );
    });
},

Find the element, click it, clear it, then send the keys.

This page has a complete code sample and article that may help.

晚风撩人 2024-09-17 12:56:12

这是很容易做到的事情,并且对我有用:

//Create a Javascript executor

JavascriptExecutor jst= (JavascriptExecutor) driver;
jst.executeScript("arguments[1].value = arguments[0]; ", 55, driver.findElement(By.id("id")));

55 = 分配的值

This is something easy to do and it worked for me:

//Create a Javascript executor

JavascriptExecutor jst= (JavascriptExecutor) driver;
jst.executeScript("arguments[1].value = arguments[0]; ", 55, driver.findElement(By.id("id")));

55 = value assigned

天煞孤星 2024-09-17 12:56:12

当我必须处理嵌入 JavaScript 的 HTML 页面时,这解决了我的问题

WebElement empSalary =  driver.findElement(By.xpath(PayComponentAmount));
Actions mouse2 = new Actions(driver);
mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform();

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onchange()", empSalary);

This solved my problem when I had to deal with HTML page with embedded JavaScript

WebElement empSalary =  driver.findElement(By.xpath(PayComponentAmount));
Actions mouse2 = new Actions(driver);
mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform();

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onchange()", empSalary);
七婞 2024-09-17 12:56:12
 WebElement p= driver.findElement(By.id("your id name"));
 p.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
 WebElement p= driver.findElement(By.id("your id name"));
 p.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文