简体   繁体   English

WhatsApp 使用 Python Selenium 的多行消息

[英]MultiLine Message With Python Selenium for WhatsApp

I've found a solution (example at this link How to line break in WhatsApp with Selenium when sending a message?我找到了一个解决方案(此链接中的示例如何在发送消息时使用 Selenium 在 WhatsApp 中换行?

But I've a problem sending Multiline Message with WhatsApp using Python and Selenium.但是我在使用 Python 和 Selenium 通过 WhatsApp 发送多行消息时遇到了问题。

This is My code :这是我的代码:

message = excel_data['Message'][msg]
# Locate search box through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))

# Clear search box if any contact number is written in it
person_title.clear()

# Send contact number in search box
person_title.send_keys(str(excel_data['Contact'][count]))
count = count + 1
msg=msg+1

# Wait for 2 seconds to search contact number
time.sleep(2)

try:
    # Load error message in case unavailability of contact number
    element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
    # Format the message from excel sheet
    message = message.replace('{customer_name}', column)
    person_title.send_keys(Keys.ENTER)
    actions = ActionChains(driver)
    actions.send_keys(message)
    actions.send_keys(Keys.ENTER)
    actions.perform()

I've a file excel with 2 column : 1° Column Phone Number and 2° Column the message我有一个包含 2 列的 Excel 文件:1° 列电话号码和 2° 列消息

All work well if message is a single message.如果消息是单个消息,则一切正常。 If message is on multi line doesn't work.如果消息在多行上不起作用。

Ex.:前任。:

Message =
Hello
Gundam How are you?
I'm well

WhataApp send 3 message : WhataApp 发送 3 条消息:

First with Hello
Second with Gundam How are you?
Third with I'well

I need all in One message in multiline我需要多行一体式消息

Could you help me modifying my code ?你能帮我修改我的代码吗?

I tried adding this but doesn't work:我尝试添加此但不起作用:

ActionChains(driver).send_keys(message).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()

Thanks a lot for your help非常感谢你的帮助

Use selenium Keys:使用硒键:

from selenium.webdriver.common.keys import Keys

Then:然后:

val="text\text"
val =val.replace("\n",(Keys.SHIFT+Keys.ENTER))

just replace '\\n' or '\\r\\n' with (Keys.SHIFT+Keys.ENTER)只需将 '\\n' 或 '\\r\\n' 替换为 (Keys.SHIFT+Keys.ENTER)

so in your case:所以在你的情况下:

First check what is the line end character首先检查什么是行尾字符

 print((message).encode("unicode_escape"))

Then replace that with Keys.shift+enter然后用 Keys.shift+enter 替换它

 message=message.replace("\n",(Keys.SHIFT+Keys.ENTER))

You can directly use the unicode charactes:您可以直接使用 unicode 字符:

 elem.send_keys("first\ue008\ue007second")

I have already explain multiple line breaking ways on a question.我已经在一个问题上解释了多种换行方式。 You can check my Answer here.你可以在这里查看我的答案。

Thank you谢谢

following functions hopefully solve your purpose.以下功能有望解决您的目的。 Actually in whatsapp, pressing Enter Key will send the message immediately so won't work for multiline.实际上在 whatsapp 中,按 Enter 键会立即发送消息,因此不适用于多行。 for multiline shift + enter required to press at same time.对于多行shift + enter需要同时按下。

Selenium ActionChains method used to overcome the problem. Selenium ActionChains 方法用来克服这个问题。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webd`enter code here`river.common.action_chains import ActionChains

def wa_msg_send(driver, msg):
    messages = msg.split('\n')
    for line in messages:
        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()
    else:
        ActionChains(driver).send_keys(Keys.RETURN).perform()

functions argument driver can be created using following line & msg is your full multiline message.可以使用以下行创建函数参数驱动程序& msg是您的完整多行消息。

driver = webdriver.Chrome(chrome_driver, options=chrome_options)

Please check and let me know if not worked.请检查并让我知道如果不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM