9

I have a multiline text and when I am simply putting the whole text into a form using sendKeys, the form gets submitted on each line break.

I tried replacing the newline with carriage return this way:

String myText="Some Multiline Text....";
myText=myText.replace("\n","\13");

This simply removed the newlines and I could not see the newline in output text.

Also below did not work(it also submits form at line breaks):

String myText="Some Multiline Text....";
myText=myText.replace("\n","\r");

So how do I go about with newlines in sendkeys without submitting the form?

rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • The literal `"\13"` is in octal, and is `chr(11)` which is a vertical tab. A line feed is `"\n"` or `"\u000a"`, and a carriage return is `"\r"` is `"\u000d"`. – Tamas Hegedus Nov 18 '15 at 15:26
  • @hege_hegedus I tried that on basis of this answer http://stackoverflow.com/a/11540219/1291122 – rahulserver Nov 18 '15 at 15:36
  • I think they might made a mistake. I can't find it in the documentation, but I think that should be `"\\13"`, so the string literal doesn't get escaped. – Tamas Hegedus Nov 18 '15 at 15:56
  • @hege_hegedus yeah it should be `"\\13"`. FYI I also tried with double slashes :p So I would suggest you to go ahead and edit that post to get some extra points ;) – rahulserver Nov 18 '15 at 16:27

3 Answers3

15

This is not a Selenium issue, pressing enter in a text field often submits the form. Usually you can bypass it by using Shift+Enter to insert a new line. Try this:

String myText = "first line\nsecond line";
myText = myText.replace("\n", Keys.chord(Keys.SHIFT, Keys.ENTER));
myElement.sendKeys(myText);
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • 1
    Not working with python 3.5, #imported below class from selenium.webdriver.common.keys import Keys But getting error "AttributeError: type object 'Keys' has no attribute 'chord'" – Dr_Dang May 26 '18 at 10:29
  • 2
    @Dr_Dang This is Java code, not python. The python bindings do not have `Keys.chord`, use an `ActionChain` instead. https://stackoverflow.com/questions/33915900/error-type-object-keys-has-no-attribute-chord – Tamas Hegedus May 26 '18 at 15:26
  • Is there any alternative for this in python3 ? – Dr_Dang May 29 '18 at 08:54
  • @Dr_Dang yes, by using `ActionChain`. Please ask a new SO question specific to python3, so we can answer it properly. – Tamas Hegedus May 29 '18 at 20:42
6

What worked for me using python 3 was make use of ActionChain as Tamas said and @Arount posted on Python and Selenium - Avoid submit form when send_keys() with newline

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get('http://foo.bar')

inputtext = 'foo\nbar'
elem = driver.find_element_by_tag_name('div')
for part in inputtext.split('\n'):
    elem.send_keys(part)
    ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()
Leonardo Wolter
  • 131
  • 1
  • 4
  • 1
    this works, but for my multi line (say 100+) comment, the time taken to create the message is considerable. As it is an activity done in DOM. Can it be sped up? Like pasting just one time would be perfect – Rahul Kahale Jun 09 '21 at 14:34
  • this is the only working solution I have seen so far. Thanks – Yingxu He Jul 26 '23 at 11:48
0

You can also use the following method for selenium. I added 2 samples. Msgbox and sendkeys

Dim myText As String = "hello\nYes"
myText = myText.Replace("\n", Environment.NewLine)
MsgBox(myText)
exDriver.FindElement(By.TagName("input")).SendKeys(myText)

Output

imgur output

Salih Can
  • 72
  • 1
  • 6