【问题标题】:How to line break in WhatsApp with Selenium when sending a message?发送消息时如何使用 Selenium 在 WhatsApp 中换行?
【发布时间】:2019-10-21 22:56:27
【问题描述】:

消息发送函数:

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

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

【问题讨论】:

  • @JeffC bcs 我找到了解决方案

标签: python-3.x selenium whatsapp


【解决方案1】:

解决了这个问题

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

【讨论】:

  • 感谢 osam 的回答。拯救我的一天。
  • 这行得通,但对于我的多行(比如 100+)评论,创建消息所花费的时间相当可观。因为它是在 DOM 中完成的活动。可以提速吗?就像只粘贴一次就完美了
【解决方案2】:

您可以使用以下代码添加该行。它工作正常,我正在我的 ERP 中使用它。

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

【讨论】:

    【解决方案3】:

    这对我有用。基本上每次出现新行时按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)
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2017-02-04
      • 2017-12-12
      • 2018-09-01
      • 2020-10-25
      • 2020-11-13
      相关资源
      最近更新 更多