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.

12
  • Does this answer your question? Fast writing in a textBox with selenium and python and here Commented Jan 15, 2020 at 14:00
  • @Sers, You do not read Please no solution with javascript execution. ? Commented Jan 15, 2020 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? Commented Jan 15, 2020 at 14:24
  • That is my question. I will not change my question to stick with your answer, it is not my need. Commented Jan 15, 2020 at 14:26
  • No one ask you to change your question. Good luck Commented Jan 15, 2020 at 14:29

4 Answers 4

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)
            {

            }
Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

0

you can set the value directly using js script:

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

4 Comments

You do not read Please no solution with javascript execution. ?
@sgrillon sendkeys was build to work sequential, the only way to make it "fast" is with js, simply because selenium have no access to the dom directly, actually, all the dom manipulations are made through js
my question is how to reduce the time between each send key (by configuration)? No by javascript.
I find my elements via an xpath, I don't necessarily have the id
0

/!\ 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");

7 Comments

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?
@Christine, I note Caution, is it a workaround only., my question is still valid if you have a solution.
@sgrillon I tried multiple Javascript solutions, none worked. This WORKED like a CHARM. Thanks for your 'Caution, is it a workaround only' solution.
It is no different than the typical sendKeys.
@mr-possible, if you past a very big text, the execution time is very long by typical sendKeys. Is it the subject of this stackoverflow question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.