Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在gmail中打开每封电子邮件?_Java_Selenium_Webdriver_Gmail - Fatal编程技术网

Java 如何在gmail中打开每封电子邮件?

Java 如何在gmail中打开每封电子邮件?,java,selenium,webdriver,gmail,Java,Selenium,Webdriver,Gmail,你能帮我看一下吗 我需要打开收件箱中的每封电子邮件 从中获取内容 public void main(){ 驱动程序。获取(“https://mail.google.com"); //gmail登录 driver.findElement(By.id(“电子邮件”)).sendKeys(“电子邮件”); driver.findElement(By.id(“next”))。单击(); driver.findElement(By.id(“Passwd”)).sendKeys(“密码”); driver

你能帮我看一下吗

  • 我需要打开收件箱中的每封电子邮件
  • 从中获取内容
    public void main(){
    驱动程序。获取(“https://mail.google.com");
    //gmail登录
    driver.findElement(By.id(“电子邮件”)).sendKeys(“电子邮件”);
    driver.findElement(By.id(“next”))。单击();
    driver.findElement(By.id(“Passwd”)).sendKeys(“密码”);
    driver.findElement(By.id(“signIn”))。单击();
    List unreademeil=driver.findElements(By.xpath(“/*[@class='zF']);
    //我要检查的邮件者姓名收件箱中是否有电子邮件
    字符串MyMailer=“КöааааGmail”;
    //真正的逻辑从这里开始
    对于(int i=0;i
    以上代码适用于多部分消息的纯文本,请在您的for循环中包含以下代码

    Multipart multipart = (Multipart) message.getContent();
    for (int j = 0; j < multipart.getCount(); j++) {
    BodyPart bodyPart = multipart.getBodyPart(j);
    System.out.println("Body: "+bodyPart.getContent());
    content= bodyPart.getContent().toString();
    System.out.println(content);
    }
    
    Multipart Multipart=(Multipart)message.getContent();
    对于(int j=0;j

    如果您的问题由此得到解决,则让我们进行更新。

    我对代码进行了一些修改。一切似乎都按预期工作

    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    public class email {
    
    private WebDriver driver;
    
    @BeforeMethod
    public void beforeMethod() {
    
        String exePath = "chromedriver_win32\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", exePath);
        driver = new ChromeDriver();
    
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://google.com/");
    
    }
    
    @AfterMethod
    public void afterMethod() {
        driver.quit();
    }
    
    @Test
    public void main() throws InterruptedException {
    
        String email = "email";
        String password = "password";
    
        driver.get("https://mail.google.com");
        driver.findElement(By.id("Email")).sendKeys(email);
        driver.findElement(By.id("next")).click();
        driver.findElement(By.id("Passwd")).sendKeys(password);
        driver.findElement(By.id("signIn")).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 = "FROMTESTEMAIL";
        String bodyemail = "";
        int i = 0;
    
            for (i = 0; i < unreademeil.size(); i++) {
                if (unreademeil.get(i).isDisplayed() == true) {
    
                    unreademeil.get(i).click();
                    System.out.println(bodyemail);
    
                    driver.findElement(By.xpath("//a[contains(text(),'Click here')]")).click();
    
                    Thread.sleep(5000);
                    System.out.println("Your current page is: " + driver.getTitle());
    
                    ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
                    driver.switchTo().window(tabs2.get(0));
                    driver.close();
                    driver.switchTo().window(tabs2.get(1));
    
                    System.out.println("Your current page is: " + driver.getTitle());
                    driver.findElement(By.xpath("/html/body/div/div[5]/div[2]/div[1]/a")).click();
                    System.out.println("It is started");
                    Thread.sleep(3000);
    
                  // do something after clicking on the required link
                  // ...  
    
                    try {
                        Alert confirmationAlert = driver.switchTo().alert();
                        String alertText = confirmationAlert.getText();
                        System.out.println("Alert text is " + alertText);
                        confirmationAlert.accept();
                    } catch (Exception e) {
                        System.out.println("The alerts haven't been found");
                        e.printStackTrace();
                    }
    
                    driver.get("https://mail.google.com");
                    Thread.sleep(5000);
                    i--;
                    unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
                }
             }
          }
       }
    
    import org.openqa.selenium.Alert;
    导入org.openqa.selenium.By;
    导入org.openqa.selenium.WebDriver;
    导入org.openqa.selenium.WebElement;
    导入org.openqa.selenium.chrome.ChromeDriver;
    导入org.testng.annotations.AfterMethod;
    导入org.testng.annotations.BeforeMethod;
    导入org.testng.annotations.Test;
    导入java.util.ArrayList;
    导入java.util.List;
    导入java.util.concurrent.TimeUnit;
    公共类电子邮件{
    私有网络驱动程序;
    @预处理法
    方法()之前的公共无效{
    字符串exePath=“chromedriver\u win32\\chromedriver.exe”;
    System.setProperty(“webdriver.chrome.driver”,exePath);
    驱动程序=新的ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.manage().window().maximize();
    驱动程序。获取(“https://google.com/");
    }
    @后置法
    公共方法(){
    driver.quit();
    }
    @试验
    public void main()引发InterruptedException{
    String email=“email”;
    字符串password=“password”;
    驱动程序。获取(“https://mail.google.com");
    驱动程序.findElement(通过.id(“电子邮件”)).sendKeys(电子邮件);
    driver.findElement(By.id(“next”))。单击();
    driver.findElement(By.id(“Passwd”)).sendKeys(密码);
    driver.findElement(By.id(“signIn”))。单击();
    //现在,将未阅读的电子邮件从收件箱放入列表
    List unreademeil=driver.findElements(By.xpath(“/*[@class='zF']);
    //我要检查的邮件者姓名收件箱中是否有电子邮件
    字符串MyMailer=“FROMTESTEMAIL”;
    字符串bodyemail=“”;
    int i=0;
    对于(i=0;i
    package Bots;
    
    import javax.mail.*;
    import java.util.Properties;
    
    public class checkemail {
    
        public static void check(String host, String storeType, String user,
                                 String password)
        {
            try {
    
                //create properties field
                Properties properties = new Properties();
    
                properties.put("mail.pop3.host", host);
                properties.put("mail.pop3.port", "995");
                properties.put("mail.pop3.starttls.enable", "true");
                Session emailSession = Session.getDefaultInstance(properties);
    
                //create the POP3 store object and connect with the pop server
                Store store = emailSession.getStore("pop3s");
    
                store.connect(host, user, password);
    
                //create the folder object and open it
                Folder emailFolder = store.getFolder("INBOX");
                emailFolder.open(Folder.READ_ONLY);
    
                // retrieve the messages from the folder in an array and print it
                Message[] messages = emailFolder.getMessages();
                System.out.println("messages.length---" + messages.length);
    
                for (int i = 0, n = messages.length; i < n; i++) {
                    Message message = messages[i];
                    System.out.println("---------------------------------");
                    System.out.println("Email Number " + (i + 1));
                    System.out.println("Subject: " + message.getSubject());
                    System.out.println("From: " + message.getFrom()[0]);
                    System.out.println("Text: " + message.getContent().toString());
                    //System.out.println("Message" + message.getDescription().toString());
    
                }
    
                //close the store and folder objects
                emailFolder.close(false);
                store.close();
    
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
    
            String host = "pop.gmail.com";// change accordingly
            String mailStoreType = "pop3";
            String username = "test@gmail.com";// change accordingly
            String password = "test";// change accoredingly
    
            check(host, mailStoreType, username, password);
    
        }
    
    }
    
    message.getContent().toString()
    
    Multipart multipart = (Multipart) message.getContent();
    for (int j = 0; j < multipart.getCount(); j++) {
    BodyPart bodyPart = multipart.getBodyPart(j);
    System.out.println("Body: "+bodyPart.getContent());
    content= bodyPart.getContent().toString();
    System.out.println(content);
    }
    
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    public class email {
    
    private WebDriver driver;
    
    @BeforeMethod
    public void beforeMethod() {
    
        String exePath = "chromedriver_win32\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", exePath);
        driver = new ChromeDriver();
    
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("https://google.com/");
    
    }
    
    @AfterMethod
    public void afterMethod() {
        driver.quit();
    }
    
    @Test
    public void main() throws InterruptedException {
    
        String email = "email";
        String password = "password";
    
        driver.get("https://mail.google.com");
        driver.findElement(By.id("Email")).sendKeys(email);
        driver.findElement(By.id("next")).click();
        driver.findElement(By.id("Passwd")).sendKeys(password);
        driver.findElement(By.id("signIn")).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 = "FROMTESTEMAIL";
        String bodyemail = "";
        int i = 0;
    
            for (i = 0; i < unreademeil.size(); i++) {
                if (unreademeil.get(i).isDisplayed() == true) {
    
                    unreademeil.get(i).click();
                    System.out.println(bodyemail);
    
                    driver.findElement(By.xpath("//a[contains(text(),'Click here')]")).click();
    
                    Thread.sleep(5000);
                    System.out.println("Your current page is: " + driver.getTitle());
    
                    ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
                    driver.switchTo().window(tabs2.get(0));
                    driver.close();
                    driver.switchTo().window(tabs2.get(1));
    
                    System.out.println("Your current page is: " + driver.getTitle());
                    driver.findElement(By.xpath("/html/body/div/div[5]/div[2]/div[1]/a")).click();
                    System.out.println("It is started");
                    Thread.sleep(3000);
    
                  // do something after clicking on the required link
                  // ...  
    
                    try {
                        Alert confirmationAlert = driver.switchTo().alert();
                        String alertText = confirmationAlert.getText();
                        System.out.println("Alert text is " + alertText);
                        confirmationAlert.accept();
                    } catch (Exception e) {
                        System.out.println("The alerts haven't been found");
                        e.printStackTrace();
                    }
    
                    driver.get("https://mail.google.com");
                    Thread.sleep(5000);
                    i--;
                    unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
                }
             }
          }
       }