Search code examples
javagmailwebdriverselenium-webdrivertestng

How to type Gmail Body text in Selenium2 (Webdriver) using Java


I tried to automate sending email from Gmail (https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2) by using Selenium WebDriver with Java. First I tried to record the test by using Selenium IDE. IDE failed to record the body of email. I tried by the following way to type on body text, but it failed unfortunately.

driver.findElement(By.xpath("//textarea[@name='body']")).sendKeys("body text");

Error is: FAILED: testSendingEmail org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.02 seconds

Can anybody please help me?


Solution

  • The following is the piece of HTML code for typing gmail body:

    <iframe frameborder="0" style="padding: 0pt; height: 218px; background-color: white;" class="Am Al editable" id=":4z" tabindex="1"></iframe>
    

    I have written the following java code in WebDriver to type gmail body and it worked nicely. (I am happy)

    WebDriver driver = new FirefoxDriver();
    WebElement frame1 = driver.findElement(By.xpath("//iframe[@class='Am Al editable']"));
    driver.switchTo().frame(frame1);
    WebElement editable = driver.switchTo().activeElement();
    String mailBody = "Hi," + '\n' + "I'm Ripon from Dhaka, Bangladesh.";
    editable.sendKeys(mailBody);
    driver.switchTo().defaultContent();