发送消息时如何使用Selenium在WhatsApp中换行? [英] How to line break in WhatsApp with Selenium when sending a message?

查看:74
本文介绍了发送消息时如何使用Selenium在WhatsApp中换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

消息发送功能:

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 中它会排成一行:

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:

文字.更多文字.更多文字.

如果你添加 \n 那么每一行都将发送一条新消息,例如:

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

发送消息截图

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

How can I send text with line breaks in one 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()

这篇关于发送消息时如何使用Selenium在WhatsApp中换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆