使用java使用selenium webdriver从Gmail收件箱中打开邮件

时间:2016-04-21 04:14:23

标签: java selenium xpath selenium-webdriver incoming-mail

我需要在Eclipse IDE中使用java使用selenium webdriver从Gmail收件箱中打开邮件。有没有办法用xpath做到这一点?

6 个答案:

答案 0 :(得分:3)

理想的方法是不使用selenium自动化Gmail,而是使用Gmail API(https://developers.google.com/gmail/api/#how_do_i_find_out_more)来验证邮件是否已成功发送。如果您不想学习如何在API级别检查消息,我强烈建议您使用此链接使用HTML版本的gmail作为gmail(https://mail.google.com/mail/?ui=html)的初始URL,使用启用了javascript的gmail将使其成为可能有一个可靠的测试脚本要困难得多。

答案 1 :(得分:1)

您好请尝试以下代码仅检查未读邮件

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);
        }
    }
}

}

答案 2 :(得分:1)

这是我的解决方案,没有任何thread.sleap()等。

   driver.get("https://mail.google.com/");                                                                                                                        

   WebElement userElement = wait.until(ExpectedConditions.elementToBeClickable(By.id("identifierId")));                                                           
   userElement.click();                                                                                                                                           
   userElement.clear();                                                                                                                                           
   userElement.sendKeys(properties.getProperty("username"));                                                                                                      

   WebElement identifierNext = wait.until(ExpectedConditions.elementToBeClickable(By.id("identifierNext")));                                                      
   identifierNext.click();                                                                                                                                        

   WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.name("password")));                                                         
   passwordElement.click();                                                                                                                                       
   passwordElement.clear();                                                                                                                                       
   passwordElement.sendKeys(properties.getProperty("password"));                                                                                                  

   WebElement passwordNext = wait.until(ExpectedConditions.elementToBeClickable(By.id("passwordNext")));                                                          
   passwordNext.click();                                                                                                                                          

   WebElement composeElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='button' and (.)='Compose']")));                            
   composeElement.click();                                                                                                                                        

   WebElement maximizeEmailElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//td//img[2]")));                                               
   maximizeEmailElement.click();                                                                                                                                  

   WebElement sendToElement = wait.until(ExpectedConditions.elementToBeClickable(By.name("to")));                                                                 
   sendToElement.click();                                                                                                                                         
   sendToElement.clear();                                                                                                                                         
   sendToElement.sendKeys(String.format("%s@gmail.com", properties.getProperty("username")));                                                                     

   WebElement subjectElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@name = 'subjectbox']")));                                        
   subjectElement.click();                                                                                                                                        
   subjectElement.clear();                                                                                                                                        
   subjectElement.sendKeys(properties.getProperty("email.subject"));                                                                                              

   WebElement emailBodyElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role = 'textbox']")));                                         
   emailBodyElement.click();                                                                                                                                      
   emailBodyElement.clear();                                                                                                                                      
   emailBodyElement.sendKeys(properties.getProperty("email.body"));                                                                                               

   WebElement sendMailElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Send']")));                                            
   sendMailElement.click();                                                                                                                                       

   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Message sent')]")));                                                   
   List<WebElement> inboxEmails = wait.until(ExpectedConditions.visibilityOfAllElements(driver.findElements(By.xpath("//*[@class='zA zE']"))));                   

   for(WebElement email : inboxEmails){                                                                                                                           
       if(email.isDisplayed() && email.getText().contains("email.subject")){                                                                                                                                   
           email.click();                                                                                                                                         

           WebElement label = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@title,'with label Inbox')]")));                    
           WebElement subject = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(),'Subject of this message')]")));          
           WebElement body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Single line body of this message')]")));   

       }                                                                                                                                                          
   }       

答案 3 :(得分:0)

包package1; import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.chrome.ChromeDriver;

public class class1 {
    public static void main(String[] args) throws InterruptedException{
        System.setProperty("webdriver.chrome.driver","C:\\Users\\name\\Desktop\\chromedriver.exe");
        ChromeDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("https://accounts.google.com/ServiceLogin/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
        driver.findElement(By.id("identifierId")).sendKeys("****@gmail.com");
        driver.findElement(By.id("identifierNext")).click();
        driver.findElement(By.xpath("//input[@aria-label='Enter your password' and @name='password']")).sendKeys("********");
        Thread.sleep(200);
        driver.findElement(By.id("passwordNext")).click();


    }}

答案 4 :(得分:0)

我同意@sonhu并做同样的事情。使用JAVAX MAIL API(不是GMAIL API)对这个问题进行了排序。

<select value='{{ u.reg_status }}'>
  <option value='mailed' {{ "selected='selected'" if u.reg_status == 'mailed' else "" }}>mailed</option>
  <option value='register' {{ "selected='selected'" if u.reg_status == 'register' else "" }}>register</option>
  <option value='not register' {{ "selected='selected'" if u.reg_status == 'not register' else "" }}>not register</option>
 </select>

答案 5 :(得分:-1)

//open a mail from the gmail inbox.
List<WebElement> a = driver.findElements(By.xpath("//*[@class='yW']/span"));
            System.out.println(a.size());
            for (int i = 0; i < a.size(); i++) {
                System.out.println(a.get(i).getText());
                if (a.get(i).getText().equals("Support")) //to click on a specific mail.
                    {                                           
                    a.get(i).click();
                }
            }