Wednesday 30 November 2011

Hello World! WebDriver with Python on Win 7

You will need:
1.  python-2.7.2.msi (Python installer)
        After installing, be sure to add  installed directory to your PATH environment.
2. setuptools-0.6c11.win32-py2.7.exe (Package installer)
       After installing, be sure to add  directory that contain easy_install.exe to your PATH environment variable. In my case it was C:\Python27\Lib\site-packages.






3. chromedriver.exe (Need this for automating in Chrome browser)
       Download chromedriver_win_16.0.902.0.zip from http://code.google.com/p/chromium/downloads/list . Remember the location of the chromedriver.exe


4. Text editor(any)
       I am using SciTE. Download it from http://www.scintilla.org/SciTEDownload.html
Start the application, select python from language. To run the code press F5.




Steps: 
1. Install Selenium. In Command Prompt write easy_install selenium
2. Start writing code


In Chrome:



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

driver = webdriver.Chrome("E:\QA\Resource\WEBDRIVER\chromedriverserver\chromedriver.exe")
driver.get("https://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Hello World!")

Here 

E:\QA\Resource\WEBDRIVER\chromedriverserver\chromedriver.exe  was my location of 
chromedriver.exe.


In Firefox:



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

driver = webdriver.Firefox()
driver.get("https://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Hello World!")

In Internet Explorer:



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

driver = webdriver.Ie()
driver.get("https://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Hello World!")

Be sure that checked Enable Protected Mode. To do that go to Internet option, select Security tab.