MultiLine Message With Python Selenium for WhatsApp

Viewed 1137

I've found a solution (example at this link How to line break in WhatsApp with Selenium when sending a message?

But I've a problem sending Multiline Message with WhatsApp using Python and Selenium.

This is My code :

message = excel_data['Message'][msg]
# Locate search box through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))

# Clear search box if any contact number is written in it
person_title.clear()

# Send contact number in search box
person_title.send_keys(str(excel_data['Contact'][count]))
count = count + 1
msg=msg+1

# Wait for 2 seconds to search contact number
time.sleep(2)

try:
    # Load error message in case unavailability of contact number
    element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
    # Format the message from excel sheet
    message = message.replace('{customer_name}', column)
    person_title.send_keys(Keys.ENTER)
    actions = ActionChains(driver)
    actions.send_keys(message)
    actions.send_keys(Keys.ENTER)
    actions.perform()

I've a file excel with 2 column : 1° Column Phone Number and 2° Column the message

All work well if message is a single message. If message is on multi line doesn't work.

Ex.:

Message =
Hello
Gundam How are you?
I'm well

WhataApp send 3 message :

First with Hello
Second with Gundam How are you?
Third with I'well

I need all in One message in multiline

Could you help me modifying my code ?

I tried adding this but doesn't work:

ActionChains(driver).send_keys(message).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()

Thanks a lot for your help

3 Answers

Use selenium Keys:

from selenium.webdriver.common.keys import Keys

Then:

val="text\text"
val =val.replace("\n",(Keys.SHIFT+Keys.ENTER))

just replace '\n' or '\r\n' with (Keys.SHIFT+Keys.ENTER)

so in your case:

First check what is the line end character

 print((message).encode("unicode_escape"))

Then replace that with Keys.shift+enter

 message=message.replace("\n",(Keys.SHIFT+Keys.ENTER))

You can directly use the unicode charactes:

 elem.send_keys("first\ue008\ue007second")
  • In the whatsapp web '\n' works an 'Enter' key
  • And 'Enter' key works as send message on whatsapp web.
  • Because of that your multi-line message is getting send in mult-parts (or as multiple message)
  • For avoid it you can change '\n' with shift+Enter Key.

You can use the following way to send message with break line in a single message

from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library which be suitable for you.

br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)      # Assigning the keys for break line
## You can use any one way from the following.

message = f"Dear Student,{br}Please send your report{br}Thank you for your attention"
####################### Or #########################
# message = "Dear Student,{0}Please send your report{0}Thank you for your attention".format(br)
####################### Or #########################
# message = "Dear Student," + br + "Please send your report" + br + "Thank you for your attention"

Or, You can use escape character as following example

message = """Hello        \
Gundam How are you?       \
I'm well """

I have already explain multiple line breaking ways on a question. You can check my Answer here.

Thank you

following functions hopefully solve your purpose. Actually in whatsapp, pressing Enter Key will send the message immediately so won't work for multiline. for multiline shift + enter required to press at same time.

Selenium ActionChains method used to overcome the problem.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webd`enter code here`river.common.action_chains import ActionChains

def wa_msg_send(driver, msg):
    messages = msg.split('\n')
    for line in messages:
        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()
    else:
        ActionChains(driver).send_keys(Keys.RETURN).perform()

functions argument driver can be created using following line & msg is your full multiline message.

driver = webdriver.Chrome(chrome_driver, options=chrome_options)

Please check and let me know if not worked.

Related