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);
  • Indeed the alternative to sendKeys would be to set the value of the input DOM element directly. However, you show only fragment of how you try to do it and you don't explain how the program "crashes" exactly. ("Crashes" is not precise at all.) – Louis Aug 30 '14 at 15:01
  • Sorry, I updated the Error message in the edit. – F. Rakes Aug 30 '14 at 15:05
  • Your code is still showing only fragments of how you do it. You don't show how input gets a value. You do show how inputField gets a value but not input, which is a different variable. – Louis Aug 30 '14 at 15:12
  • That was unfortunately a typo. It should be inputField throughout. With .value = longstring the program executes fine, but doesn't actually set the value. – F. Rakes Aug 30 '14 at 15:19
  • Similar for Python: Set attribute of an element using webdriver at SQA – kenorb May 23 '15 at 19:56
up vote 38 down vote accepted

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

webdriver.executeScript("document.getElementById('elementID').setAttribute('value', 'new value for element')");
  • I get the same error as when I try to do .value = longstring Error: ElementNotVisibleError: element not visible – F. Rakes Aug 30 '14 at 15:39
  • Could you please add sample of your html? Because for google this code work excellent. Possibly problem with your element on page (it's hidden i think) – Andrey Egorov Aug 30 '14 at 16:03
  • I'm actually using this example to test it with google. Could you put your code that worked for you here, please? – F. Rakes Aug 30 '14 at 16:22
  • here you are. driver.executeScript("document.getElementById('gbqfq').setAttribute('value','Selenium Web Driver')"); – Andrey Egorov Aug 31 '14 at 2:12
  • 4
    In Python Selenium it's execute_script – Chris Hawkes Jul 10 '15 at 12:38

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')");
  • What's new in your answer in comparison with previous answers? – Stepan Novikov Oct 19 '17 at 21:05
  • @Stepan - not much, but none of the answers were in C# which is what I was looking for but couldn't find it anywhere. So here it is. It could help someone like me in the future. – Leojet Oct 23 '17 at 15:56

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'")

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.

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);
  • 9
    Yeah, this also will work. But also can be modified, to be more simplier: driver.executeScript("arguments[0].setAttribute('value', arguments[1])", inputField, longstring); – Andrey Egorov Aug 31 '14 at 4:00
  • 2
    driver.executeScript("arguments[0].value = '" + longstring + "'", inputField) is another possible improvement that also works with textAreas (I've only tested in Chrome, though) – Johannes Brodwall Aug 25 '15 at 20:11
  • A slight modification to the above 2 ideas because at least in my context I was trying to set a textarea and it complained about the syntax. This is in protractor: browser.executeScript("arguments[0].value=arguments[1].toString()", inputField, longstring); I also found that to make the field update, it helped to then inputField.sendKeys(" "), where in my context the extra space was harmless. – Jeremy Kahan Mar 8 at 6:20

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.