Set value of input instead of sendKeys() - Selenium WebDriver nodejs

Javascriptnode.jsSeleniumSelenium Webdriver

Javascript Problem Overview


I have a long string to test and sendKeys() takes too long. When I tried to set the value of the text the program crashes. I know the Selenium sendKeys() is the best way to test the actual user input, but for my application it takes too much time. So I am trying to avoid it.

Is there a way to set the value right away?

See this quick example:

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
      build();

driver.get('http://www.google.com');

// find the search input field on google.com
inputField = driver.findElement(webdriver.By.name('q'));

var longstring = "test"; // not really long for the sake of this quick example

// this works but is slow
inputField.sendKeys(longstring);

// no error but no values set
inputField.value = longstring;

// Output: TypeError: Object [object Object] has no method 'setAttributes'

inputField.setAttributes("value", longstring);

Javascript Solutions


Solution 1 - Javascript

Try to set the element's value using the executeScript method of JavascriptExecutor:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");

Solution 2 - Javascript

Extending from the correct answer of Andrey-Egorov using .executeScript() to conclude my own question example:

inputField = driver.findElement(webdriver.By.id('gbqfq'));
driver.executeScript("arguments[0].setAttribute('value', '" + longstring +"')", inputField);

Solution 3 - Javascript

Thanks to Andrey Egorov, in my case with python setAttribute not working, but I found I can set the property directly,

Try this code:

driver.execute_script("document.getElementById('q').value='value here'")

Solution 4 - Javascript

An alternative way of sending a large number of repeating characters to a text field (for instance to test the maximum number of characters the field will allow) is to type a few characters and then repeatedly copy and paste them:

inputField.sendKeys('0123456789');
for(int i = 0; i < 100; i++) {
    inputField.sendKeys(Key.chord(Key.CONTROL, 'a'));
    inputField.sendKeys(Key.chord(Key.CONTROL, 'c'));
    for(int i = 0; i < 10; i++) {
        inputField.sendKeys(Key.chord(Key.CONTROL, 'v'));
    }
}

Unfortunately pressing CTRL doesn't seem to work for IE unless REQUIRE_WINDOW_FOCUS is enabled (which can cause other issues), but it works fine for Firefox and Chrome.

Solution 5 - Javascript

Thanks to Andrey-Egorov and this answer, I've managed to do it in C#

IWebDriver driver = new ChromeDriver();
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string value = (string)js.ExecuteScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");

Solution 6 - Javascript

In a nutshell, this is the code which works for me :)

WebDriver driver;
WebElement element;
String value;

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='"+ value +"';", element);

Solution 7 - Javascript

If you want to use some variable, you may use this way:

String value= "your value";
driver.execute_script("document.getElementById('q').value=' "+value+" ' ");

Solution 8 - Javascript

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.querySelector('attributeValue').value='new value'");

Solution 9 - Javascript

Thanks guys, here's how I've managed to do this in Java

public static void sendKeysJavascript(By element, String keysToSend) {
    WebElement el = driver.findElement(element);
    JavascriptExecutor ex = (JavascriptExecutor) driver;
    ex.executeScript("arguments[0].value='"+ keysToSend +"';", el);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionF. RakesView Question on Stackoverflow
Solution 1 - JavascriptAndrey EgorovView Answer on Stackoverflow
Solution 2 - JavascriptF. RakesView Answer on Stackoverflow
Solution 3 - JavascriptLarry SongView Answer on Stackoverflow
Solution 4 - JavascriptDaveView Answer on Stackoverflow
Solution 5 - JavascriptLeojetView Answer on Stackoverflow
Solution 6 - JavascriptharmiderView Answer on Stackoverflow
Solution 7 - JavascriptNavraj JoshiView Answer on Stackoverflow
Solution 8 - JavascriptRakeshView Answer on Stackoverflow
Solution 9 - JavascriptGustavoView Answer on Stackoverflow