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

2024-02-21

消息发送功能:

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,必要的答案被获取并传递给send_message() as the answer争论。 如果您按原样发送消息,那么在 WhatsApp 中它会出现在一行中:

Text.More text.Much more text.

如果你添加\n然后每一行都会发送一条新消息,即:

已发送消息的屏幕截图 https://i.stack.imgur.com/1mA9k.jpg

如何在一条消息中发送带有换行符的文本?


解决了这个问题

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

发送消息时如何使用 Selenium 在 WhatsApp 中换行? 的相关文章

随机推荐