使用selenuim webdriver从excel vba发送whatsapp消息

问题描述 投票:0回答:2

当我尝试发送多行消息时,它将单独发送每一行。我如何一次发送整条消息。 这是我的代码

Sub WhatsAppMessage()

    'Activate Selenuim web driver
    Dim bot As New WebDriver
    Dim ks As New Keys
    Dim mob As String
    Dim text As String
    
    'Init new Chrome Instance & navigate to webwhatsapp
    bot.Start "Chrome", "https://web.whatsapp.com/"
    bot.Get "/"
    
    'Ask user to scan the QR code. Once Logged in, continue with the macro
    'MsgBox "Please Scan QR code, After you are Logged in, please confirm this message box by clicking OK"
    
    'Determine number of messages by identifying the number of last rows in column A
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Search mobile number / name, Press enter, paste text into whatsapp, press enter to send message
    For i = 4 To LastRow
        'Get mobile number or name from worksheet
        mob = Sheets("Data").Cells(i, 1).Value
        
        'Get Message from Worksheet
        text = Sheets("Data").Cells(2, 1).Value
        
        'click in the searchBox
        bot.FindElementByXPath("//*[@id='side']/div[1]/div/div/div[2]").Click
        
        'wait 500ms
        bot.Wait (500)
        
        'Insert mobile number or name
        bot.SendKeys (mob)
        
        'wait 500ms
        bot.Wait (500)
        
        'press enter to confirm search mobile
        bot.SendKeys (ks.Enter)
        
        'wait 500ms
        bot.Wait (500)
        
        'Load message in to web whatsapp
        bot.SendKeys (text)
        
        'wait 500ms
        bot.Wait (500)
        
        'press enter to send message
        bot.SendKeys (ks.Enter)
        
    Next i
    MsgBox "Done :)"
End Sub

这是我想一次性发送的消息,但它会单独发送每一行

Hello!!
Dear Shree Deeplaxmi Customers,

Exclusive Once in a Year Sale Event - Up to 50% Off! Limited Stock!Join us for an incredible sale starting from July 16, 2023.

Enjoy discounts from 15% to 50% on a wide range of products!

Hurry and take advantage of this limited-time offer. Stock is limited, and once it's gone, the sale ends. Don't miss out on the opportunity to grab your desired items at unbeatable prices.Visit our store soon and make the most of this exclusive discount. 

We look forward to serving you and providing an exceptional shopping experience.See you at our sale event!

Best regards,
Sandeep Sakunde
Shree Deeplaxmi Readymade
excel vba selenium-webdriver whatsapp sendmessage
2个回答
0
投票

对于阅读本文的任何人,请向我保证您不会使用它向人们发送垃圾邮件,好吗?好吧...

我从未使用过 WhatsApp,但消息传递应用程序将 ENTER 解释为“提交”是很常见的。

在这里,Selenium 只是天真地模仿您在键盘上执行的操作来重现提供的文本,并在遇到换行符时发送 ENTER。我假设,像大多数消息应用程序一样,WhatsApp 支持消息内的换行符,但您必须按 SHIFT+ENTER 以避免立即发送消息。

要使用 Selenium 执行此操作,您可以逐行拆分为一个数组,一次键入一行,并在每个行的末尾发送 SHIFT+ENTER。例如:

    'Split the text by line
    Dim LinesOfText As Variant
    LinesOfText = Split(SomeText, vbNewLine)
    
    Dim i As Long
    For i = LBound(LinesOfText) To UBound(LinesOfText)
        MyInput.SendKeys LinesOfText(i) 'Type a single line
        If i <> UBound(LinesOfText) Then
            MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
        End If
    Next i
    MyInput.SendKeys keyObj.Enter 'Submit form

这是使用 jsfiddle 的完整演示:

Sub TextAreaInput()

    Dim MyWebDriver As WebDriver
    Set MyWebDriver = New WebDriver
    MyWebDriver.Start "chrome"
    MyWebDriver.Get "https://jsfiddle.net/decimalturn/ka0zmrfq/8/"
    DoEvents
    
    'This line is needed to get the iframe where the demo html is located
    MyWebDriver.SwitchToFrame MyWebDriver.FindElementByCss("[name='result']")
    
    Dim MyInput As WebElement
    Set MyInput = MyWebDriver.FindElementById("myInput")
    
    Dim keyObj As Selenium.keys
    Set keyObj = New Selenium.keys
    
    'Short text
    MyInput.SendKeys "Some text"
    MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
    MyInput.SendKeys "Some text"
    MyInput.SendKeys keyObj.Enter 'Submit form
    
    'Long text
    Dim SomeText As String
    SomeText = "Some text" & vbNewLine & "Some more text" & vbNewLine & "Even more text"
    
    'Split the text by line
    Dim LinesOfText As Variant
    LinesOfText = Split(SomeText, vbNewLine)
    
    Dim i As Long
    For i = LBound(LinesOfText) To UBound(LinesOfText)
        MyInput.SendKeys LinesOfText(i) 'Type a single line
        If i <> UBound(LinesOfText) Then
            MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
        End If
    Next i
    MyInput.SendKeys keyObj.Enter 'Submit form
    
    Stop 'Stop execution to look at the result
    MyWebDriver.Close
    Set MyWebDriver = Nothing

End Sub

请注意,简单的

Replace(text, vbNewLine, keyObj.Shift & keyObj.Enter)
不起作用,因为 Shift 键是粘性的,因此,从第二行开始,所有文本都将是大写的。

相关问题:


0
投票

DecimalTurn 走在正确的轨道上,但他的代码对我来说不起作用:正如他所期望的“从第二行开始,所有文本都将是大写的”。 因此,在按预期构建新行和多行消息后,我只需按下 Shift 键即可。

MyInput.SendKeys keyObj.Shift & keyObj.Enter 'New line
MyInput.SendKeys keyObj.Shift
© www.soinside.com 2019 - 2024. All rights reserved.