Click on the 'compose' button in gmail inbox page

463 views Asked by At

I want to click on the 'compose' button in gmail inbox page.

My script is not able to although I have given a valid username and password.

    WebDriver driver=new FirefoxDriver();
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
    driver.findElement(By.id("Email")).sendKeys("username");
    driver.findElement(By.id("next")).click();
    Thread.sleep(1000);
    WebElement Err=driver.findElement(By.xpath("//*[@id='errormsg_0_Email']"));
    String errtext=Err.getText();

     if(!Err.isDisplayed()){
        driver.findElement(By.id("Passwd")).sendKeys("password");
        driver.findElement(By.id("signIn")).click();
        Thread.sleep(1000);
        List<WebElement> errorDivs = driver.findElements(By.xpath("//div[@class='error-msg']"));

        if(errorDivs.isEmpty()){
            driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click();
            System.out.println("User loged in successfully");               
        }else{
             errorDivs.get(0).getText();
            driver.quit();
        }               
     }else{
         System.out.println("wrong password");
     }
}
3

There are 3 answers

14
Vikas Ojha On

In Gmail the 'ids' of elements are never consistent.

Change your code for finding of 'Compose' button from

driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click();

to

driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();

Hope that helps.

0
Aru On

I am able to run your script after 2 changes:

1.Comment driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS); ,you used 2 times in your script and specified 500 so it takes time.You can use explicit wait for specific element.Please read [this][1] .

2.Replace driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click(); with driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();, which is suggested by Vikas.



  [1]: http://www.toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/
0
Santoshsarma On

Here is the code for login & sending mail from Gmail

//load login page
driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en");

//login logic
driver.findElement(By.id("Email")).sendKeys(userName);
driver.findElement(By.cssSelector("[value='Next']")).click();
driver.findElement(By.id("Passwd")).sendKeys(password);
driver.findElement(By.id("signIn")).click();

//click on Compose button
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();

//typing subject & to address
driver.findElement(By.xpath("//*[text()='To']/../../..//textarea")).sendKeys(to);
driver.findElement(By.name("subjectbox")).sendKeys(subject);


//click on Send
driver.findElement(By.xpath("//div[text()='Send']")).click();

P.S : You need to put proper waits at different places to elements load properly. Otherwise above code will throw exception.