How to line break in WhatsApp with Selenium when sending a message?

十年热恋 提交于 2021-01-28 11:09:12

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/56477290/how-to-line-break-in-whatsapp-with-selenium-when-sending-a-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!