Monday, 19 December 2011

Simple Gmail login automation with Selenium WebDriver



Simple Steps:


1. Go to  http://mail.google.com
2. Enter your user name 'yourgmailid'
3. Enter your password 'yourpassword'
4. Login
5. Goto Sent mail
6. Logout




Simple Code:



import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys




driver= webdriver.Chrome("E:\QA\Resource\WEBDRIVER\chromedriverserver\chromedriver.exe")
driver.get("http://mail.google.com")


emailid=driver.find_element_by_id("Email")
emailid.send_keys("yourgmailid")


passw=driver.find_element_by_id("Passwd")
passw.send_keys("yourpassword")


signin=driver.find_element_by_id("signIn")
signin.click()


time.sleep(10)


driver.switch_to_frame('canvas_frame')


sentmail= driver.find_element_by_link_text('Sent Mail')
sentmail.click()


time.sleep(10)


sentmail= driver.find_element_by_link_text('Your Name')
sentmail.click()


lout= driver.find_element_by_link_text('Sign out')
lout.click()

Simple explanation:

Select your browser
driver= webdriver.Chrome("E:\QA\Resource\WEBDRIVER\chromedriverserver\chromedriver.exe")
can also use
driver= webdriver.Firefox() or
driver= webdriver.Ie()


Choose your destination:
driver.get("http://mail.google.com")


Find element:
emailid=driver.find_element_by_id("Email")


how?
<input id="Email" type="text" value="" name="Email" spellcheck="false">


or?


<input id="Email" type="text" value="" name="Email" spellcheck="false">
emailid=driver.find_element_by_name("Email")


more? YES

find_element_by_xpath
find_element_by_css_selector
find_element_by_class_name
find_element_by_link_text
etc


Enter text:
remember from selenium.webdriver.common.keys import Keys?
emailid.send_keys("yourgmailid")


Handle iframe:
driver.switch_to_frame('canvas_frame') 
How?


For first part visit Hello World! WebDriver with Python on Win 7





11 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Wrong comment earlier. Very good initiative :)

    ReplyDelete
  3. I used these lines in ruby:

    @selenium = Selenium::SeleniumDriver.new 'localhost' , 4444, '*firefox' , 'http://www.gmail.com' , 10000

    @selenium.type "Email", "yourid"
    @selenium.type "Passwd", "yourpassword"
    @selenium.click "//input[@value='Sign in']"
    @selenium.wait_for_page_to_load "30000"
    sleep 5
    @selenium.click "link=Sent Mail"
    @selenium.click "link=yourprofilename"
    @selenium.click "link=Sign out"
    @selenium.wait_for_page_to_load "30000"

    ReplyDelete
  4. আল আরমান I guess you are using Selenium RC, right?

    For Webdriver ruby bindings

    require 'selenium-webdriver'
    driver = Selenium::WebDriver.for :firefox

    ReplyDelete
  5. by the way after 'sleep 5'
    you need to select iframe 'canvas_frame', when exporting code from IDE this part always skipped. :)

    ReplyDelete
  6. Merci
    aswqergh
    Probably one of the most talked about stores in Paris,Isabel Marant Sneakers

    ReplyDelete
  7. wesofttech.com Call 1-888-505-6485 (Toll Free) for Gmail Technical Support.Gmail Password Recovery, Gmail Tech Support, Gmail support number and Gmail Help etc.

    Visit Link- Gmail Support

    ReplyDelete
  8. hi thanks to this writer for this nice blog and this good information
    other useful link - Gmail Tech Support

    ReplyDelete
  9. I have a smart solution for this using Selenium IDE. I could go to composer text area by just using its class (class=editable LW-avf). For detailed article, please refer to http://www.seleniumhq.in/2013/04/automatically-login-to-gmail-and-send.html

    ReplyDelete