0

I need to send Large text in the type of String to textArea element. I use

driver.findElement(By.xpath(textarea_xpath)).sendKeys(fileText);

But Its too slow. Any ideas to make this fast?

6
  • How large is large? There's not really another way to send it unless you use straight JS but that probably won't be faster either. You could try sending smaller chunks of the text using .sendKeys() but that shouldn't go faster. Commented Sep 19, 2021 at 21:48
  • No Way. Selenium bahaves like this. typing will be very slow. Commented Sep 20, 2021 at 6:15
  • How about ctrl+c and ctrl+v, not sure if it will be bit faster or not. Commented Sep 20, 2021 at 6:52
  • @cruisepandey Yes copying the content to the clipboard and the paste it in the textArea is faster then sendKeys in selenuim. but the problem is that this willn't work once i use headless mode in the chrome driver Commented Sep 20, 2021 at 7:13
  • element.sendKeys(String string) takes approximately 8 seconds for string of leghth 10000 chars. Which is acceptable. How big is your string? Commented Sep 20, 2021 at 9:46

3 Answers 3

1

You can try it with JS executor:

  public void enterTextJS(By locator, String text) {
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(locator);
    jsExecutor.executeScript("arguments[0].value='" + text + "';", element);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Your code works only when text is one line string and fails when it is a multiline string So it will be better to use this instead jsExecutor.executeScript("arguments[1].value=arguments[0]", "'" + text + "'", element);
1
 public void enterTextJS(By locator, String text) 
{
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(locator);
    jsExecutor.executeScript("arguments[1].value=arguments[0]", "'" + text + "'", element);}

This worked for me like a charm. For multiple lines of text, this is the best solution. Thanks @omar_Hafez

1 Comment

In my case, the value disappears once the focus moves to the next field.
0

Basicaly same solution as Villa_7's. Setting text of 6k chars takes ~ 25 miliseconds.

package selenium;

import java.time.LocalDateTime;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class OmarHafez extends WebDriverSetup {

    public static void main(String[] args) {

        WebDriver driver = startChromeDriver();
        driver.get("https://demoqa.com/automation-practice-form");
        WebElement addressField = driver.findElement(By.tagName("textarea"));
        addressField.click();
        String s = randomStringOfLength(6000);
        System.out.println(LocalDateTime.now());
        String js = "document.evaluate(\"//textarea[@id='currentAddress']\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = '" + s + "';";
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor)driver;
        javascriptExecutor.executeScript(js);
        System.out.println(LocalDateTime.now());
        System.out.println(addressField.getAttribute("value"));
        driver.quit();
    }
    
    public static String randomStringOfLength(int length) {
        int leftLimit = 48; // numeral '0'
        int rightLimit = 122; // letter 'z'
        Random random = new Random();
        String generatedString = random.ints(leftLimit, rightLimit + 1)
          .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
          .limit(length)
          .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
          .toString();
        return generatedString;
    }

}

Comments

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.