[英]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<mpl=default<mplcache=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<mpl=default<mplcache=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 您还可以使用附件发送邮件
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.