1. Moving To a Selenium Example

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

driver = webdriver.Firefox()
driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("Myname")
elem.send_keys(Keys.ENTER)
driver.close() 


1. The selenium.webdriver module provides all the WebDriver implementations.

2. The Keys class provide keys in the keyboard like RETURN, ENTER,F1, ALT etc.
 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

Next, the instance of Firefox WebDriver is created.
 
driver = webdriver.Firefox()

The driver.get method will navigate to a page given by the URL ie."http://www.google.com". WebDriver will wait until the page has fully loaded  before returning control to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded.
 
driver.get("http://www.google.com")

WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. For example, the input text element can be located by its name attribute using find_element_by_name method.
 .

elem = driver.find_element_by_name("q")
 
For locating the element learn xpath and css. These are very good technique to locating element in a web page 

Next we are sending keys, this is similar to entering keys using your keyboard. Special keys can be send using Keys class imported from selenium.webdriver.common.keys:
 
elem.send_keys("Myname")
elem.send_keys(Keys.ENTER)

After submission of the page, you should get the result if there is any:
Finally, the browser window is closed. You can also call quit method instead of close. The quit will exit entire browser where as close will close one tab, but if it just one tab, by default most browser will exit entirely.:
driver.close() 
 
                                 

No comments: