11

I am using Python 3 with selenium.

Let's assume var = "whatever\nelse"

My problem is that when I use elem.send_keys(var) it sends the form after "whatever" (because of the newline)

How may I replace "whatever\nelse" with whatever + SHIFT+ENTER + else?

Or is there any other way to input newlines without actually using javascript or substituting newlines with the newline keystroke?

Note: elem is a contenteditable div.

Álvaro N. Franz
  • 1,188
  • 3
  • 17
  • 39
  • is it a textarea or input ? I checked with textarea and it works perfectly fine. – Gaurang Shah Jul 21 '17 at 11:28
  • @GaurangShah it is a contenteditable div in a form. Why so? Because of a WYSIWYG editor. – Álvaro N. Franz Jul 21 '17 at 11:29
  • it could be something wrong with that div, probably check with your dev. I check on the w3chool and it works perfectly with `contenteditable p`. you can check that too. https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_global_contenteditable – Gaurang Shah Jul 21 '17 at 11:31
  • @GaurangShah Thank you. The problem is that the div is not mine. It belongs to the whatsapp web platform. – Álvaro N. Franz Jul 21 '17 at 11:46

1 Answers1

15

Did you tried something like:

ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()

Like

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

ActionChains will chain key_down of SHIFT + ENTER + key_up after being pressed.

Like this you perform your SHIFT + ENTER, then release buttons so you didn't write all in capslock (because of SHIFT)

PS: this example add too many new lines (because of the simple loop on inputtext.split('\n'), but you got the idea.

Arount
  • 9,853
  • 1
  • 30
  • 43
  • Python says: AttributeError: 'WebElement' object has no attribute 'key_down' – Álvaro N. Franz Jul 21 '17 at 11:42
  • hm, let me check – Arount Jul 21 '17 at 11:44
  • Thank you for your time. Your answer looks like the perfect approach so far. – Álvaro N. Franz Jul 21 '17 at 11:45
  • yes, but I've messed it :p Was too confident about my selenium skills :) PS: fixed – Arount Jul 21 '17 at 11:47
  • It still will not create the new lines. I put a `chat.send_keys("NEW")` before every new line, just to make sure the action is being taken. But it prints everything in a line like. "text par 1. NEW. text part 2..." Also, if I do SHIFT + ENTER manually, it works fine. Meaning that `ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER)` is not working. But there is no traceback. – Álvaro N. Franz Jul 21 '17 at 11:54
  • 3
    It works fine if you change the last both key strokes. As: `ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.ENTER).key_up(Keys.SHIFT).perform()` – Álvaro N. Franz Jul 21 '17 at 12:01
  • For me it worked as suggested by @ÁlvaroN.Franz, by releasing ENTER before SHIFT... – Felipe Ferri Jul 15 '18 at 00:50
  • @ÁlvaroN.Franz I am trying to do this but I get a strange error, and my text is written multiple times almost randomly. Can you look at my question: [link](https://stackoverflow.com/questions/66133462/selenium-with-python-newline-with-shiftenter-works-but-messes-up-the-text) – Benjamin Carafa Feb 10 '21 at 09:28
  • 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:36