How to type Gmail Body text in Selenium2 (Webdriver) using Java

限于喜欢 提交于 2019-12-04 15:47:48

YES.. you can't record the body of email using Selenium IDE.

include the following method in your project and call that method to send Email.(No need to login into gmail)

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public void SendEmail()
{

  // Recipient's email ID needs to be mentioned.
  String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Now set the actual message
     message.setText("This is actual message");

     // Send message
     Transport.send(message);
     //System.out.println("Sent message successfully....");
  }
catch (MessagingException mex) {
     mex.printStackTrace();
  }
}

You can also send mail with Attachments

Refer this link for more information.

when using class it throws and error element not found its better to use table index.

        WebElement frame1 = driver.findElement(By.xpath("//iframe[@tabindex='1']"));
    driver.switchTo().frame(frame1);
    WebElement editable = driver.switchTo().activeElement();
    String mailBody = "Hi," + '\n' + "Gmail Body";
    editable.sendKeys(mailBody);
    driver.switchTo().defaultContent();

The following is the piece of HTML code for typing gmail body:

<iframe frameborder="0" style="padding: 0pt; height: 218px; background-color: white;" class="Am Al editable" id=":4z" tabindex="1"></iframe>

I have written the following java code in WebDriver to type gmail body and it worked nicely. (I am happy)

WebDriver driver = new FirefoxDriver();
WebElement frame1 = driver.findElement(By.xpath("//iframe[@class='Am Al editable']"));
driver.switchTo().frame(frame1);
WebElement editable = driver.switchTo().activeElement();
String mailBody = "Hi," + '\n' + "I'm Ripon from Dhaka, Bangladesh.";
editable.sendKeys(mailBody);
driver.switchTo().defaultContent();

Try out below code to write in body area

driver.findElement(By.cssSelector("body[class='editable  LW-avf']")).clear();
driver.findElement(By.cssSelector("body[class='editable  LW-avf']")).sendKeys("body text");

WebDriver driver= new FirefoxDriver();

WebElement text=driver.findElement(By.className("LW-avf"));

text.click(); text.sendKeys("Hello");

Please try using above code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!