SeleniumJava :删除Gmail中的未读邮件



我正在做硒练习,想从我的Gmail收件箱中删除大约30K封未读邮件。 尝试了很多定位器和xpath但是我的xpath正在选中所有邮件复选框。

任何人都可以建议如何选择未读邮件复选框

PFB Java Selenium Code

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Unread_Gmail {
public static void main(String[] args) throws InterruptedException {
    WebDriver driver;
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1#identifier");
driver.findElement(By.id("Email")).sendKeys("*********@gmail.com");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("******");
driver.findElement(By.id("signIn")).click();

Thread.sleep(3000);

driver.findElement(By.xpath("//div[@class='G-tF']/div[1]/div")).click();
driver.findElement(By.xpath(".//*[@id=':z2']/div")).click();
//driver.findElement(By.xpath("//div[@class='J-J5-Ji J-JN-M-I-Jm'][1]")).click();
//driver.findElement(By.xpath("//div[@class='J-J5-Ji J-JN-M-I-Jm']")).click();
//driver.findElement(By.id("z2")).click();
//driver.findElement(By.xpath("//div[@class='J-N-Jz']")).click();
//driver.findElement(By.id("z2")).click();
}
}

你熟悉Gmail API吗?如果您只想练习定位器等,找到复选框是要走的路,但在"现实生活"中,当您必须验证/测试 smtp 服务器时,使用 API 是要走的路。每封电子邮件(线程(都有一个标签,调用消息未读将返回未读的消息。您还可以使用查询,该查询在 API 和 UI 中:

in:inbox is:unread

就我而言,"未读"下拉选项的定位器如下所示:

<div class="J-N" selector="unread" role="menuitem" id=":dz" style="user-select: none;">
    <div class="J-N-Jz" style="user-select: none;">Unread</div>
</div>

看起来您需要查找类为"J-N"和/或 id ":d z"的元素。

您可以

尝试提供元素的编号,例如12,然后尝试单击。

driver.findElements(By.xpath("//*[@role='checkbox']")).get(1).click();
//JavaMail is an API which can be used to handle different operations in selenium.
 public static void main(String [] args){
        String to="";
        final String user=""; 
        final String password="xxxxx"; 
        
        //1) get the session object
        Properties properties = System.getProperties(); 
        properties.setProperty("mail.smtp.host", "mail.javatpoint.com"); 
        properties.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthenticationgetPasswordAuthentication(){ 
                return new PasswordAuthentication(user,password);
            }
        });
      
       // add code here to handle mails 
       }
    

最新更新