How to add text to body of e-mail?

Viewed 234

I searched StackOverflow and others with no good results.

I have to send e-mails through Access VBA. I'm using Selenium Basic library.

I can, access the e-mail web page, click on the "new mail" button, fill fields "To", "subject", "Copy to" and "blind copy to" if such parameters are given, and click the "send e-mail" button.

I cannot, edit the rich text body of the e-mail. I can send e-mails, but they're blank.

Public Sub sendEmail( _
    emailPage As String, _
    sendTo As String, _
    subject As String, _
    emailText As String, _
    Optional copyTo As String, _
    Optional blindCopyTo As String)
    
'Parameters above: emailPage is the intranet, corporative e-mail site from which the e-mails must be 
'sent; sendTo is the e-mail address to which the e-mail will be sent; subject is the subject of the 
'e-mail; emailText is the text that I want to go in the body of the e-mail. 

'Variables below: preparing variables to be used, like a webDriver. I tried to mingle Selenium and 
'Windows HTML objects to no vail, as one can infer from the commented variable

    Dim browser As WebDriver
    Dim webEle As WebElement
    Dim theKeys As selenium.Keys
    Dim por As By
    'Dim CurrentWindow As HTMLWindowProxy
    
'choosing the web browser driver. I chose firefox since it went farther than chrome*:

    Set browser = New FirefoxDriver

'with the browser driver, I go to the e-mail webpage:

    With browser
        .Get emailPage 

'Below I find the elements with the XPath to interact with

        .SwitchToFrame "s_MainFrame"
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[10]/table[1]/tbody/tr/td[2]/table/tbody/tr/td[1]/span").Click
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[3]/td[3]/div/textarea").SendKeys sendTo
        If IsMissing(copyTo) = False Then
            .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[4]/td[3]/div/textarea").SendKeys copyTo
        End If
        If IsMissing(blindCopyTo) - False Then
            .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[5]/td[3]/div/textarea").SendKeys blindCopyTo
        End If
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[6]/td[3]/div/textarea").SendKeys subject

'And here is the problem: I cannot access the e-mail body text to change it. SendKeys won't work. I also
'tried inserting HTML texts like <HTLM><BODY>email text goes here</BODY></HTML> and 
'<HTLM><H1>email text goes here</BODY></HTML>, to no vail.

        .FindElementByXPath("/html/body/div/div").SendKeys emailText
        '.SwitchToFrame "s_MainFrame"

'After that, I click the button to send the e-mail, close the browser driver, and set it to nothing.
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[14]/table[1]/tbody/tr/td[1]/span").Click
    End With
    browser.Close
    Set browser = Nothing
   
End Sub

These are the elements that I get when inspecting the body text. In the first line of the e-mail body text, which is clean, the element is

<div><br></div>

And its XPath is:

/html/body/div/div

But using it as the path to access is of no use in my tests. Below is the rich text body text element main values:

<body role="main" id="e-$new-0-bodyrich-editor" class="s-mailbody-edit s-disable-inotesstyle">

And it's XPath:

/html/body

In Chrome, I get an error, and it doesn't open the email page. I have the Chrome 87 version and the appropriate driver.

1 Answers

Sorry to bother everyone!

I did some more testing and discovered I had to reference the XPath for the iframe:

    .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[2]/div/div/div[1]/iframe").SendKeys emailText

I also updated the function with a While DoEvents to close the browser driver only after the e-mail page writes in the referenced HTML element that the e-mail was sent succesfully:

    Do While InStr(1, browser.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[2]/div[1]/div/div[2]/div/table[1]/tbody/tr/td[2]/span").text, "Email sent successfully") = 0
        DoEvents
    Loop
    

Also, some declared variables were unused and were removed, like the webEle. Only the browser variable is declared in the final code.

Related