Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SeleniumWebDriver:我想覆盖字段中的值,而不是使用Java将sendKeys附加到字段中_Java_Selenium Webdriver_Sendkeys - Fatal编程技术网

SeleniumWebDriver:我想覆盖字段中的值,而不是使用Java将sendKeys附加到字段中

SeleniumWebDriver:我想覆盖字段中的值,而不是使用Java将sendKeys附加到字段中,java,selenium-webdriver,sendkeys,Java,Selenium Webdriver,Sendkeys,在WebDriver中,如果我使用sendKeys,它会将我的字符串附加到字段中已经存在的值。我不能用clear方法清除它,因为第二次我这样做时,网页会抛出一个错误,说它必须在10到100之间。所以我无法清除它,否则在使用sendKeys输入新值之前会抛出错误,如果我使用sendKeys,它只会将它附加到已经存在的值上 WebDriver中是否有任何内容允许您覆盖字段中的值?我认为您可以尝试先选择字段中的所有文本,然后发送新序列: from selenium.webdriver.common.k

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


WebDriver中是否有任何内容允许您覆盖字段中的值?

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

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

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

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

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

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清除它。它将尝试通过空路径查找文件并抛出错误


您可以找到更多详细信息好的,几天前。。。 在我目前的情况下,来自ZloiAdun的答案对我来说不起作用,但使我非常接近我的解决方案

而不是:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
以下代码让我很高兴:

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

所以我希望这能帮助一些人

如果这对任何人都有帮助,那么ZloiAdun的答案的C等价物是:

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

这解决了我在处理带有嵌入式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);
这对我有用

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

由于textfield不接受键盘输入,并且鼠标解决方案似乎不完整,因此使用上述大多数方法时出现问题

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

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

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

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

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 );
    });
},
找到元素,单击它,清除它,然后发送键

这可能会有所帮助。

使用以下方法:

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

这是一件容易做到的事情,对我来说很有效:

//Create a Javascript executor

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

55=赋值

是的,这很有效,我只需要在类中导入Keys。非常感谢。我正在派顿试一试。如何导入密钥?@BaoNgo对于Mac,您需要使用Keys.COMMAND而不是Keys.CONTROL。@SergiiPozharov是否有与此命令等效的Python?我使用了send_keys,但我得到了一个错误AttributeError:type对象“keys”没有属性“chord”,我找到了它。element.send_keysKeys.CONTROL,“a”对不起,我理解错了。我的解决方案-只是关于清除输入值的正确方法,而不是关于如何用问题中提到的新的一对一actionTrue_Blue替换当前值,他不能使用clear,因为他第二次这样做时,网页会抛出一个错误,说它必须在10到100之间。打得好。我将在这里保留我的答案,以防将来有人会用到,因为标题没有指定。如果此解决方案对某人不起作用,请提供其他信息:在调用element.clear之前,确保element.GetAttributevalue确实有一个值。请等待此值为非空。它有时发生在使用ngModel指令测试AngularJS输入时。对我来说很有用,即使字段为空。我使用了这个->元素。sendKeysKeys.HOME,Keys.chordKeys.SHIFT,Keys.ARROW_DOWN,Keys.ARROW_DOWN,Keys.ARROW_DOWN;这太神奇了,它在一个文本区域框中选择了3行文本,因为命令a对我不起作用。这应该更好,Ctrl+a在西班牙语键盘/窗口中可能不起作用。虽然这段代码可能提供了一个问题的解决方案,但最好添加上下文,说明为什么/如何工作。这可以帮助未来的用户学习,并将这些知识应用到他们自己的代码中。在解释代码时,用户可能会以投票的形式向您提供正面反馈。
 WebElement p= driver.findElement(By.id("your id name"));
 p.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");