2

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

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Christian
  • 21
  • 3
  • 2
    "*It doesn't work*" isn't an acceptable problem statement. **How** didn't it work? What happened when you tried using that (improperly indented) snippet? Were there any errors? If so, please post the full text *as text in the question itself*. Please [edit] your question and put together a full [mre] that we can run and get the same behavior as you are getting. The code that you have posted isn't runnable, as it contains several errors and undefined variables. Help us help you. – MattDMo Jan 05 '21 at 23:32

3 Answers3

3

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")
PDHide
  • 18,113
  • 2
  • 31
  • 46
0
  • 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

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/30640207) – holydragon Dec 21 '21 at 08:30
  • You are right, But because of less time I just refer the link to see there all the types which I have used till now for break line. And I am not sure about which way I have to post as an answer here. Because I have no idea which way is suitable for the user to use. – Anurag Kushwaha Dec 21 '21 at 09:02
  • By the way thanks @holydragon to tell about adding a short answer. I have answered in short to use. If there is any mistake or something wrong then let me know. – Anurag Kushwaha Dec 21 '21 at 09:13
  • This seems to be in essence the same solution as the already existing and upvoted answer. How does this answer improve on that? – buddemat Dec 21 '21 at 13:36
  • I am sorry if you did not get the answer given by me. But I have not did that, what you are thinking. I think if you go through the above comment then you will get that I did not write the full answer here before because the answer was already available here. I just had given the link of my answer which I have written to tell use that in many ways. Before answer this question I tried to give the link in comment, but I was unable to post a comment on the question because my reputation is less then 50. – Anurag Kushwaha Dec 22 '21 at 05:30
  • @Anurag Kushwaha, i am sorry that i did not noticed your answer wisely. thanks for your nice clarification. – ahmedul Kabir Omi Dec 25 '21 at 20:21
  • It's all fine, You are doing good to give answer on the question if there is answer available. It can help to others to get more information about the answer and find an easy way to use... – Anurag Kushwaha Dec 27 '21 at 05:18
0

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.