question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[🐛 Bug]: webElement.sendKeys carriage return to a textarea

See original GitHub issue

What happened?

I am using selenium to type multi-line text to <textarea> in html. When the text contains carriage return character (\r or U+000D), I found selenium didn’t type it in the text area (attached my reproduction code). The newline character (\n or U+000A) is typed.

According to <textarea> w3c standard, <textarea> normalizes newline remaining \r. https://html.spec.whatwg.org/#the-textarea-element:normalize-newlines https://infra.spec.whatwg.org/#normalize-newlines quote “To normalize newlines in a [string], replace every U+000D CR U+000A LF [code point] pair with a single U+000A LF [code point] and then replace every remaining U+000D CR [code point].”

When I use selenium to sendKeys a text with both \r\n and \r (e.g. “line 1\r\nline 2\rline 3”), I only see \n was typed but \r is ignored. Because if \r is also type, I would see <textarea> normalize \r\n -> \n and \r -> \n.

Without using selenium, I can prove the \r can be typed to <textarea> and is normalized to \n. This is my jsbin demo code: https://jsbin.com/xapisezini/edit?html,js,console,output

How can we reproduce the issue?

/* reproduce selenium sendKey test */
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class MyTest {
    private WebDriver driver;
    @Before
    public void setup() {
        System.setProperty("webdriver.chrome.driver","chromedriver");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testTextArea() throws InterruptedException {
        driver.get(<localhost URL to hello.html>);
        WebElement ta = driver.findElement(By.id("w3review"));
        ta.sendKeys("line 1\r\nline 2\rline 3");
    }

    @After
    public void teardown() {
        driver.quit();
    }
}

/* hello.html */
<!DOCTYPE html>
<html>
<body>
<h1>The textarea element</h1>
  <p><label>Reproduce sendKeys:</label></p>
  <textarea id="w3review"></textarea>
</body>
</html>

Relevant log output

After selenium sendKeys to <textarea>, the observed textarea is
line 1
line 2line 3

I am expecting desired textarea
line 1
line 2
line 3

Operating System

Debian 10

Selenium version

Java <groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-support</artifactId><version>2.31.0</version>

What are the browser(s) and version(s) where you see this issue?

Chrome 99

What are the browser driver(s) and version(s) where you see this issue?

<groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-chrome-driver</artifactId><version>4.4.0</version>

Are you using Selenium Grid?

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
titusfortnercommented, Sep 19, 2022

The best way to get one of the browser vendors to look at something is to write a web platform test and show that it is passing in one browser and not another… Not sure where it fits in here, but if you can PR something here it should get looked at: https://github.com/web-platform-tests/wpt/tree/master/webdriver/tests/element_send_keys

If you get it figured out, tag me in the PR or link to it here so I see it.

0reactions
github-actions[bot]commented, Oct 20, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Selenium Webdriver enter multiline text in form without ...
This is not a Selenium issue, pressing enter in a text field often submits the form. Usually you can bypass it by using...
Read more >
How to type text in a new line inside a text area using ...
Hey Unnati, '\n' is usually used to type text in a new line inside a textarea. So with Selenium Webdriver, we can achieve...
Read more >
Selenium WebDriver - How to type text in a ne
Selenium WebDriver - How to type text in a new line inside a text area ? Use \n for new line. ex- webelement.sendKeys(“Sanjay_Line1.\n...
Read more >
Selenium with Java 18 - How to use newline(\n) and tab(\t ...
How To Use NewLine (\n) And Tab(\t) Character in webelement SendKeys ?Like our facebook page www.facebook.com/ankprotrainingHow to type text ...
Read more >
TextField and TextArea | SpringerLink
Calling SendKeys() to the same text field will concatenate the new text with the old text. It is therefore a good idea to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found