简体   繁体   English

如何使用Java在Selenium2(Webdriver)中键入Gmail正文文本

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

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. 我尝试自动从Gmail发送电子邮件(https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2)使用Selenium WebDriver和Java。 First I tried to record the test by using Selenium IDE. 首先,我尝试使用Selenium IDE记录测试。 IDE failed to record the body of email. IDE无法记录电子邮件正文。 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"); 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 错误是:FAILED:testSendingEmail org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时交互:30.02秒

Can anybody please help me? 有人可以帮帮我吗?

YES.. you can't record the body of email using Selenium IDE . 是..您无法使用Selenium IDE记录电子邮件正文

include the following method in your project and call that method to send Email.(No need to login into gmail) 在项目中包含以下方法并调用该方法发送Email。(无需登录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: 以下是用于输入gmail正文的HTML代码:

<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. 我在WebDriver中编写了以下java代码来键入gmail body,它工作得很好。 (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(); WebDriver driver = new FirefoxDriver();

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

text.click(); text.click(); text.sendKeys("Hello"); text.sendKeys( “你好”);

Please try using above code. 请尝试使用上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium2(java)输入iframe? - How to type in iframe using selenium2 (java)? 如何使用Java和Selenium2 / Webdriver将明天的日期输入文本字段 - How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver 使用Java的Selenium WebDriver(aka Selenium2)中的getPageSource() - getPageSource() in Selenium WebDriver(a.k.a Selenium2) using Java 当浏览器窗口未聚焦时,使用Java的Selenium2(WebDriver)失败 - Selenium2 (WebDriver) using Java fails when browser window is not focused 无法使用带有 Java 的 Selenium2 (Webdriver) 启动 IE 浏览器 - Not able to launch IE browser using Selenium2 (Webdriver) with Java 如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本 - How to type some text in hidden field in Selenium WebDriver using Java 如何在Selenium2(Webdriver)中处理POPUP浏览器? - How to Handle POPUP browser in Selenium2(Webdriver)? 在java中使用selenium webdriver登录Gmail - Gmail login using selenium webdriver in java 如何使用chrome驱动程序使用Java覆盖selenium2中的基本身份验证? - How to override basic authentication in selenium2 with Java using chrome driver? 如何使用Selenium WebDriver(Selenium 2)和Java键入文本框? - How to type in textbox using Selenium WebDriver (Selenium 2) with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM