4

Message sending function:

template = {
    'other': 
             'Text.'
             'More Text.'
             'Much more text.'
}


def send_message(driver, answer):
    driver.find_element_by_xpath('XPATH').click()
    action = ActionChains(driver)
    action.send_keys(answer)
    action.send_keys(Keys.RETURN)
    action.perform()

Depending on the received message from the template, the necessary answer is taken and passed to send_message() as the answer argument. If you send the message as is, then in WhatsApp it comes in one line:

Text.More text.Much more text.

If you add \n then each line will be sent with a new message, i.e. like that:

screenshot of sent message

How can I send text with line breaks in one message?

kshnkvn
  • 876
  • 2
  • 18
  • 31

4 Answers4

9

Solved this

def send_message(driver, answer):
    driver.find_element_by_xpath('XPATH').click()
    for line in answer.split('\n'):
        ActionChains(driver).send_keys(line).perform()
        ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()
    ActionChains(driver).send_keys(Keys.RETURN).perform()
kshnkvn
  • 876
  • 2
  • 18
  • 31
  • Thanks for the osam answer. Save my day. – Tejas Patel Sep 17 '20 at 15:32
  • 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:38
0

You can use the following code to add the line. It is working fine and I am using it in my ERP.

smsContain = "*Greetings from  " + cname + " ,%0a %0a M/s. " + txtName.Text + " %0a %0a
renatodvc
  • 2,526
  • 2
  • 6
  • 17
Jayesh Sonpal
  • 401
  • 4
  • 2
0

This worked for me. Basically pressing SHIFT+ENTER every time a new line appears.

MESSAGE = """This is a sample message.
It accepts one new line.


also accepts multiple new lines as well.
"""

for one_line in MESSAGE.split("\n"):
    driver.find_element('').send_keys(one_line)
    driver.find_element('').send_keys(Keys.SHIFT + Keys.ENTER)
driver.find_element('').send_keys(Keys.ENTER)
Hammad
  • 529
  • 7
  • 17
0

I try with

"\n" or (Keys.Shift + Keys.Enter) or Environment.NewLine

to make multiple lines in WhatsApp Group using Selenium With C# but not work

I use Alt and it's work to send message with multiple lines on Whatsapp Group

(Keys.Alt + Keys.Enter)

Dimas
  • 39
  • 3