Python - Opening Interfacing and reading the contents of a browser using selenium in python


*******************************************************
* CREATED DATE - 1/20/2015
* MODIFIED BY - Harry
* DESCRIPTION - The script open an URL and search and display 

                                the result on console
* AUTHOR NAME - Harry Wane Jose
* LICENSE - Open source
* CREATED FOR - s4silver users
*******************************************************



The selenium.webdriver module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Chrome, Ie and Remote. The Keys class provide keys in the keyboard like RETURN, 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. WebDriver will wait until the page has fully loaded (that is, the “onload” event has fired) 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.:
5 Selenium Python Bindings, Release 2


driver.get("http://www.python.org")


The next line is an assertion to confirm that title has “Python” word in it:


assert "Python" in driver.title


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. Detailed explanation of finding elements is available in the Locating Elements chapter:


elem = driver.find_element_by_name("q")


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("pycon")
elem.send_keys(Keys.RETURN)


After submission of the page, you should get the result if there is any. To ensure that some results are found, make an assertion:


assert "No results found." not in driver.page_source


Finally, the browser window is closed. You can also call quit method instead of close. The quit will exit entire browser whereas close‘ will close one tab, but if just one tab was open, by default most browser will exit entirely.:


driver.close()



How to Install Selenium module in python 3.x


Step1 : Go to the folder path  "C:\Python34\Scripts"

Step2 : click on left shift + click on right button of mouse. A   
             list of options will come in that click on         
             "Open command window here"




 Step3 : write the following command in command window 
              and then press enter.

             pip.exe install selenium

It will automatically download and install the selenium module.


No comments:

Post a Comment