发送消息时如何使用 Selenium 在 WhatsApp 中换行?

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

消息发送函数:

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

根据从 template 收到的消息,必要的答案将作为 answer 参数传递给 send_message()。 如果您按原样发送消息,那么在 WhatsApp 中它会出现在一行中:

Text.More text.Much more text.

如果您添加 \n 那么每一行都会发送一条新消息,即像这样:

screenshot of sent message

如何在一封邮件中发送带有换行符的文本?

解决了这个问题

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

您可以使用以下代码添加该行。它运行良好,我正在我的 ERP 中使用它。

smsContain = "*Greetings from  " + cname + " ,%0a %0a M/s. " + txtName.Text + " %0a %0a

这对我有用。基本上每次出现新行时都按 SHIFT+ENTER

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)