如何使用Selenium从特定邮件程序中选择最新的未读邮件。 class / id标识符特定于特定邮件,因此导致硬编码使用这些标识符。
答案 0 :(得分:0)
除了下选票:没有共同的方法可以做到这一点。这完全取决于您的Web-Mailer。通常未读的邮件附有一些特殊类。所以我在这样的情况下做的就是寻找它们,构建一个XPath(可以在chrome dev工具搜索文本字段中轻松测试,或者使用Chrome扩展“XPath Helper”)并点击tem并删除它们一个接一个,或者 - 如果存在 - 选择同一行中的复选框。 XPath表达式往往相对较大,但也足够灵活。
最大的问题是,Web-Mailer经常会发生变化,因此您需要跟踪。这可能令人不安。
答案 1 :(得分:0)
您好,请找到您的问题的解决方案,这完全符合您最近的评论
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\eclipseProject\\StackOverFlow\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://accounts.google.com/ServiceLogin?");
// gmail login
driver.findElement(By.id("Email")).sendKeys("your gmail username");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("your gmail password");
driver.findElement(By.id("signIn")).click();
// some optional actions for reaching gmail inbox
driver.findElement(By.xpath("//*[@title='Google apps']")).click();
driver.findElement(By.id("gb23")).click();
// now talking un-read email form inbox into a list
List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
// Mailer name for which i want to check do i have an email in my inbox
String MyMailer = "Udacity";
// real logic starts here
for(int i=0;i<unreademeil.size();i++){
if(unreademeil.get(i).isDisplayed()==true){
// now verify if you have got mail form a specific mailer (Note Un-read mails)
// for read mails xpath loactor will change but logic will remain same
if(unreademeil.get(i).getText().equals(MyMailer)){
System.out.println("Yes we have got mail form " + MyMailer);
// also you can perform more actions here
// like if you want to open email form the mailer
break;
}else{
System.out.println("No mail form " + MyMailer);
}
}
}
}
和eclipse中控制台显示的结果是
Yes we have got mail form Udacity
希望这可以帮助你找到你想要的东西
答案 2 :(得分:0)
我不建议使用JAVAX MAIL API更好地浏览GUI选项。我有类似的问题,已排序。因此,使用下面的JAVAX MAIL API和Java代码:
public GmailUtils(String username, String password, String server,
EmailFolder
emailFolder) throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imaps.partialfetch", "false");
props.put("mail.imap.ssl.enable", "true");
props.put("mail.mime.base64.ignoreerrors", "true");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", 993, "<your email>", "<your password>");
Folder folder = store.getFolder(emailFolder.getText());
folder.open(Folder.READ_WRITE);
System.out.println("Total Messages:" + folder.getMessageCount());
System.out.println("Unread Messages:" + folder.getUnreadMessageCount());
messages = folder.getMessages();
for (Message mail : messages) {
if (!mail.isSet(Flags.Flag.SEEN) && mail.getFrom()[0]= <emailer address>)
// unread email and from the specific sender
{ System.out.println("**********************************");
System.out.println("MESSAGE : \n");
System.out.println("Subject: " + mail.getSubject());
System.out.println("From: " + mail.getFrom()[0]);
System.out.println("To: " + mail.getAllRecipients()[0]);
System.out.println("Date: " + mail.getReceivedDate());
System.out.println("Size: " + mail.getSize());
System.out.println("Flags: " + mail.getFlags());
System.out.println("ContentType: " + mail.getContentType());
System.out.println("Body: \n" + getEmailBody(mail));
System.out.println("Has Attachments: " + hasAttachments(mail));
}
}
}