[🐛 Bug]: webElement.sendKeys carriage return to a textarea
See original GitHub issueWhat 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:
- Created a year ago
- Comments:8 (3 by maintainers)

Top Related StackOverflow Question
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.
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.