我试图通过自动化的Gmail(https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1<mpl=default<mplcache=2)发送电子邮件使用硒的webdriver与Java。 首先,我试图通过使用Selenium IDE记录测试。 IDE没有记录电子邮件的正文。 我试图通过以下方式对身体键入文本,但不幸失败了。
driver.findElement(By.xpath( “// textarea的[@名称= '主体']”))的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 = "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();
}
}
您也可以发送带附件 的邮件
请参考以下链接了解更多信息。
使用类,它抛出和错误元素时没有发现它能够更好地使用表的索引。
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();
尝试下面的代码在身体部位写
driver.findElement(By.cssSelector("body[class='editable LW-avf']")).clear();
driver.findElement(By.cssSelector("body[class='editable LW-avf']")).sendKeys("body text");
驾驶员的webdriver =新FirefoxDriver();
WebElement文本= driver.findElement(By.className( “LW-AVF”));
text.click(); text.sendKeys( “你好”);
请尝试使用上面的代码。