1

This code work but sendKeys send all char one by one and is it very very long time (40s).

String value = "...very long text...";
WebElement element = ...
element.sendKeys(value);

How to set text in textarea quickly using Java and Selenium? either with an element of Selenium or by modifying the speed or the characters are sent one by one temorary.

Please no solution with javascript execution.

Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • Does this answer your question? [Fast writing in a textBox with selenium and python](https://stackoverflow.com/questions/52336190/fast-writing-in-a-textbox-with-selenium-and-python) and [here](https://stackoverflow.com/questions/59740310/selenium-clearing-and-typing-in-text-into-textarea-text-box-python/59742061#59742061) – Sers Jan 15 '20 at 14:00
  • @Sers, You do not read `Please no solution with javascript execution.` ? – Stéphane GRILLON Jan 15 '20 at 14:05
  • There's no any configuration you can change to speed up send keys. Why not using JS, are there any restriction and what they are? – Sers Jan 15 '20 at 14:24
  • That is my question. I will not change my question to stick with your answer, it is not my need. – Stéphane GRILLON Jan 15 '20 at 14:26
  • No one ask you to change your question. Good luck – Sers Jan 15 '20 at 14:29
  • yes a little anyway, it is well noted in the question `Please no solution with javascript execution.` – Stéphane GRILLON Jan 15 '20 at 14:38
  • There're two workarounds to do it. One is JavaScript. – Sers Jan 15 '20 at 15:15
  • the workaround is not great. I have to delete the last character and sendKeys with the last character. I'm going to do with that for the moment because there doesn't seem to be a solution in Selenium yet. – Stéphane GRILLON Jan 15 '20 at 15:30
  • not Selenium, but you could use Robot to fill the clipboard and then paste it. – pcalkins Jan 15 '20 at 19:35
  • @pcalkins, have you a sample? – Stéphane GRILLON Jan 15 '20 at 20:05
  • Fill clipboard using plain old Java: https://docs.oracle.com/javase/7/docs/api/java/awt/datatransfer/Clipboard.html Then use Robot to send keys, Ctrl-V... https://stackoverflow.com/questions/6631933/how-would-i-make-a-paste-from-java-using-the-system-clipboard – pcalkins Jan 15 '20 at 20:07
  • come to think of it, you may be able to just use WebDriver to send Ctrl-V. – pcalkins Jan 15 '20 at 20:09

4 Answers4

3

Here's a way to use the clipboard for this:

    String value = "...very long text...";      
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable transferable = new StringSelection(value);
    clipboard.setContents(transferable, null);  
    wait = new WebDriverWait(driver, ec_Timeout);     
    WebElement element  = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("name_of_input_element")));
    String vKey = "v";

            try
            {
        element.sendKeys(Keys.CONTROL , vKey);    
            }
            catch (Exception ex)
            {

            }
pcalkins
  • 1,188
  • 13
  • 20
2

The sendKeys method is the only pure Java way to enter text into a text field using Selenium. Unfortunately all other ways require JavaScript, which is something you do not want to do.

Your only other alternative is to contribute to the Selenium project on GitHub by submitting a pull request with the desired behavior, and convince the World Wide Web Consortium to include this new method (sendKeysQuickly?) in the official WebDriver spec: https://www.w3.org/TR/webdriver/ — not a small task indeed!

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
1

/!\ Caution, is it a workaround only.

String value = "...very long text...";
WebElement element = ...
String javascript = "arguments[0].value=arguments[1];";
(JavascriptExecutor) driver.executeScript(javascript, element, value.substring(0, value.length() - 1));
element.sendKeys(value.substring(value.length() - 1));

/!\ 2nd workaround (not work on remote):

String value = "...very long text..."; 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(value), null);
WebElement element = ... 
element.sendKeys(Keys.CONTROL , "v");
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • 4
    You mentioned in your question "no answers with Javascript execution" but you just posted an answer to your own question using JSE. Should this be an edit to your question instead? – CEH Jan 15 '20 at 15:51
  • @Christine, I note `Caution, is it a workaround only.`, my question is still valid if you have a solution. – Stéphane GRILLON Jan 15 '20 at 17:27
  • @sgrillon I tried multiple Javascript solutions, none worked. This WORKED like a CHARM. Thanks for your 'Caution, is it a workaround only' solution. – Dev Raj May 18 '20 at 04:21
0

you can set the value directly using js script:

  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("document.getElementById('textareaId').setAttribute('value', 'yourText')");