java - 如何使用 Java 在 Selenium2 (Webdriver) 中输入 Gmail 正文

标签 java gmail webdriver selenium-webdriver testng

我尝试自动从 Gmail 发送电子邮件 (https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1<mpl=default<mplcache= 2) 通过将 Selenium WebDriver 与 Java 结合使用。首先我尝试使用 Selenium IDE 来记录测试。 IDE 无法记录电子邮件正文。 我尝试通过以下方式输入正文,但遗憾的是失败了。

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

错误是:失败:testSendingEmail org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时交互:30.02 秒

有人可以帮我吗?

最佳答案

是的..您无法使用Selenium IDE记录电子邮件正文

在您的项目中包含以下方法并调用该方法发送电子邮件。(无需登录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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5c5f5e597d5a505c5451135e5250" rel="noreferrer noopener nofollow">[email protected]</a>";

  // Sender's email ID needs to be mentioned
  String from = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99eefcfbd9fef4f8f0f5b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>";

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

您还可以发送带有附件邮件

引用this link了解更多信息。

关于java - 如何使用 Java 在 Selenium2 (Webdriver) 中输入 Gmail 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11116577/

相关文章:

java - Red5视频流录制中断

gmail - PHPMailer 遇到问题

java - Chrome 选项 - Selenium 3.10 - NoSuchMethodError : com. google.common.collect.ImmutableList.toImmutableList()

c# - Selenium Webdriver - 如何为 Firefox 设置代理到 "auto-detect"

java - 单元测试 - 定理

java - 是否强制/推荐使用 requireExplicitBindings

java - NoUniqueBeanDefinitionException : no qualifying bean of type. 我定义了一个匹配的 bean,但我发现了 2 个

css - Mailchimp 和 Gmail : text color issue

c# - 在 C# 中从 gmail 获取通知的最佳方法

ruby - 如何使用 Selenium Ruby Webdriver 双击表格中的单元格