I was trying to build testing scripts with python for Google Chrome on my Ubuntu 16.04 machine. So i just sharing some info that how we could work with Google chrome browser through python modules. This could be an example of HowTo work on Web Browser through python selenium module for testing scripts. Python is highly rich in terms available modules and libraries which make it highly compatible for various purpose. So there could many ways to work with browser through python language and i am just discussing one of them which looks good to me.

Here we are going to use Selenium python module which has some pretty features which cover various aspects for testing scenario. In this post we will only cover how to open browser with website and fill something on it and submit.

So first we to install Selenium and Chrome driver on your machine.


For this setup , i am using Ubuntu machine 16.04 with python 2.7. You can use any machine like Unix/Linux/nix or Windows or Mac and Python version like 2.7 or 3.5

ssirohi@jarvis:~$ lsb_release -d
Description:	Ubuntu 16.04.3 LTS
ssirohi@jarvis:~$ python -V
Python 2.7.12
ssirohi@jarvis:~$ python3 -V
Python 3.5.2

>>> print selenium.__version__

Now we need to install selenium module like below.

# pip install selenium 

Now we need to download driver( packages for your browser. Like in my case i am using Google Chrome. So we can download various python driver for selenium from this link .I am using Chrome driver 2.9 from here.

Once your machine is ready we can run python cli like below.

ssirohi@jarvis:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.

As said i am using Ubuntu machine and python 2.7 as mentioned above in output. You could use any of your compatible version.

Now we need to import required module on python command line.

>>> import os
>>> from selenium import webdriver

So I have import two module, one is base OS module and another is webdriver from selenium module. Selenium itself has very rich in function and same as webdriver also. We can check various functions available in webdriver.

>>> dir(webdriver)
['ActionChains', 'Android', 'BlackBerry', 'Chrome', 'ChromeOptions', 'DesiredCapabilities', 'Edge', 'Firefox', 'FirefoxOptions', 'FirefoxProfile', 'Ie', 'Opera', 'PhantomJS', 'Proxy', 'Remote', 'Safari', 'TouchActions', 'WebKitGTK', 'WebKitGTKOptions', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', 'android', 'blackberry', 'chrome', 'common', 'edge', 'firefox', 'ie', 'opera', 'phantomjs', 'remote', 'safari', 'support', 'webkitgtk']

Now we can see webdriver module has very rich functions, cover various types of browser. In this post we are going to use Chrome, which also has huge functions cover almost every aspects of usage for testing.

>>> dir(webdriver.Chrome)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_unwrap_value', '_web_element_cls', '_wrap_value', 'add_cookie', 'application_cache', 'back', 'close', 'create_options', 'create_web_element', 'current_url', 'current_window_handle', 'delete_all_cookies', 'delete_cookie', 'desired_capabilities', 'execute', 'execute_async_script', 'execute_script', 'file_detector', 'file_detector_context', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'forward', 'fullscreen_window', 'get', 'get_cookie', 'get_cookies', 'get_log', 'get_network_conditions', 'get_screenshot_as_base64', 'get_screenshot_as_file', 'get_screenshot_as_png', 'get_window_position', 'get_window_rect', 'get_window_size', 'implicitly_wait', 'launch_app', 'log_types', 'maximize_window', 'minimize_window', 'mobile', 'name', 'orientation', 'page_source', 'quit', 'refresh', 'save_screenshot', 'set_network_conditions', 'set_page_load_timeout', 'set_script_timeout', 'set_window_position', 'set_window_rect', 'set_window_size', 'start_client', 'start_session', 'stop_client', 'switch_to', 'switch_to_active_element', 'switch_to_alert', 'switch_to_default_content', 'switch_to_frame', 'switch_to_window', 'title', 'window_handles']

Now we have to locate your download driver in python shell. Chrome driver downloaded in zip format and we have to unzip it.

ssirohi@jarvis:/images$ ls -lth chromedriver*
-rw-r–r– 1 root root 3.6M Dec 10 11:14 chromedriver_linux64.zip
-rwxr-xr-x 1 root root 7.6M Dec 9 21:44 chromedriver

So we have both, please unzip your Chrome driver and mention its path in python shell.


Above mentioned variable contain Chrome driver path which will open one chrome window,in next line we maximize chrome windows attached to python process.

Now we could open website inside Chrome from python shell


In above way we can open Google on attached Chrome.

Now we need to find element with Chrome Opened website on which we need to work.For this we need to know to see website code in Chrome, we can open this ctl+shift c keyword, which will open source code and highlight. Now we need to find through which element’s property we will need to work out. In below image i tried to show code on which we worked, Element name is q which is highlighted and mentioned in below code

So Now we have opened Google and we will try to work on Google Search tab. Here we will try to write something in search and will see results.

>>> search=driver.find_element_by_name("q")

>>> search.send_keys("python language")

>>> search.submit()

In Above commands we have try to find out one element by name and save it in variable. Further we send some key “python language” on it and submit it for search.

This whole will open Chrome windows with Google search on “python language”

In this I should summarise complete code again.

import os
from selenium import webdriver


search.send_keys("python language")