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

社会主义新天地 提交于 2019-12-21 20:46:41

问题


I tried to automate sending email from Gmail (https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2) by using Selenium WebDriver with Java. First I tried to record the test by using Selenium IDE. IDE failed to record the body of email. I tried by the following way to type on body text, but it failed unfortunately.

driver.findElement(By.xpath("//textarea[@name='body']")).sendKeys("body text");

Error is: FAILED: testSendingEmail org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.02 seconds

Can anybody please help me?


回答1:


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.




回答2:


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



回答3:


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



回答4:


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");



回答5:


WebDriver driver= new FirefoxDriver();

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

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

Please try using above code.



来源:https://stackoverflow.com/questions/11116577/how-to-type-gmail-body-text-in-selenium2-webdriver-using-java

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