Open Browser

For any browser related automation testing, opening a browser is must. Selenium doesnot have capability to work with already existing browser.

So whenever we want to test something with selenium, we have to open new browser and test it.

Open Firefox in selenium python

Till firefox 48, selenium was supporting the firefox by default but from firefox 48 version, firefox team have changed the their internal structure, so selenium stopped supporting Firefox by default.

we have to set the connecting executable file between Selenium python and Firefox browser.

We can pass the executable_path property to Firefox class constructor, to open the firefox brwser in selenium python .


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Firefox(executable_path=r'D:\PATH\geckodriver.exe');
								

Open Chrome in python

From the beginning years, Chrome provided a executable file to connect the python bindings of selenium with chrome browser


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								

Open Internet Explorer in python selenium

We can also open Internet explorer by setting correct path of the executables.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Ie(executable_path=r'D:\PATH\IEdriverserver.exe');
								


ie-open-selenium-python

Open Microsoft Edge Browser in python selenium

For opening Edge browser we have to set the driverserver executable like all other browser.

But for Microsoft edge browser we have to provide the exact driver server based on your operating system build.

You can download the edge driver server from this URL : https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
build-edge-driverserver-selenium-python

To find your correct build number: Go to Start > Settings > System > About and locate the number next to OS Build on the screen. osbuild-edge-browser-selenium-python

If you donot have correct build for the driverserver executable, then Edge browser may not work as expected


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Ie(executable_path=r'D:\PATH\IEdriverserver.exe');
								

Close and Quit Browsers

python selenium Bindings provides option to close the browser as well.

Close the browser

When call the method close() selenium bindings closes the browser window. close() method closes only the current window on which the selenium have control.

For example Selenium opened three windows and control is on third window, if call the close() method selenium closes the third window alone, window 1 and two 2 are not affected.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Edge(executable_path=r'D:\PATH\MicrosoftWebDriver.exe');
								// closes current window
								driver.close()
								

Close the browser in python selenium bindings

quit() method not only closes current window, it closes all the windows opened by the driver. In above example, if you call quit() method, it closes window 1, 2, 3

quit() method terminates the driverserver executable process as well.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Edge(executable_path=r'D:\PATH\MicrosoftWebDriver.exe');
								# closes all browser windows
								driver.quit()
								

Open Webpage in selenium python

Opening a webpage is more important part in the web application testing, we can use get() present in the selenium to open a webpage.

get() will not pass the control of program until the page loads, when page loading is finished then control goes to next line of code.

Whenever we pass the website address to the get() method, it first of checks for the protocol in the address

Selenium python throws exception If protocol is not present in the URL of the website.

What is protocol ?
Protocol is prefix to mention what kind of site is that. Examples for protocol:
http
https
ftp
file

Program to open a website


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Edge(executable_path=r'D:\PATH\MicrosoftWebDriver.exe');
								# open website
								driver.get("http://google.com")
								
open-browser-python-selenium

Work offline in python selenium

If you are not connected the internet but if you have a page, which you had stored when there was internet. You can access such kind o offline pages in selenium.

Instead of giving the website address, give the local address of the stored page.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Edge(executable_path=r'D:\PATH\MicrosoftWebDriver.exe');
								# open website
								driver.get("file:///C:/Users/path/selenium.html")
								

Browser Size in selenium python

Now a days every almost every website is developed using Bootstrap or some other responsive frameworks.

Responsive website fits into all size screens, according to the screen size, all the webelements are wrapped.

To test this kind of websites we have to change the screen size in selenium

Methods to change the screen Size

Below are few methods, with which we can manipulate the browser window sizes

1. maximize_window
2. fullscreen_window
3. set_window_size
4. set_window_rect (may not work)
5. set_window_position
6. get_window_position
7. get_window_rect (may not work)
8. get_window_size
9. minimize_window (may not work)

maximize_window

maximize_window() method in selenium python, maximizes the current browser window

Few browsers open in maximized state and few browsers may not open in maximized state. Sometimes we have scenario to be performed on maximized browser. In such cases we can use maximize_window method to maximize the window.

If you call maximize_window() method on browser which is already in maximized state, then this method will not have any effect on that browser.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Edge(executable_path=r'D:\PATH\MicrosoftWebDriver.exe');
								# open website
								driver.get("http://google.com")
								# maximize browser window
								driver.maximize_window()
								

fullscreen_window

fullscreen_window() in python selenium bindings, helps the user to set the browser size to full screen. When you make the browser to full screen, you cannot see title bar, address bar, url bar in the browser, you can only view the webpage.

By pressing F11 key, you can achieve full screen manually. Selenium will not open the full screen on browser start up itself, based on the occurrence of the fullscreen_window method; selenium changes the size to full screen.

In below example python selenium bindings will change the browser to full screen once, the browser opens the Google page.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# browser full window
								driver.fullscreen_window()
								

set_window_size(width, height, windowHandle)

set_window_size method helps the user to resize the window according to the need of the user, this method will be helpful, when you want to test the responsive website.

This method accepts the height, width of the browser also accepts which browser window we want to resize, if we donot pass browser window parameter then selenium python bindings considers the current window as the target window.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# browser window size
								driver.set_window_size(200, 300)
								

set_window_rect(x, y, width, height)

set_window_rect method not only resizes the browser window but also we can set the position of the browser window by providing the x and y coordinate.

Note: Sometimes this command may not work in new version of selenium.


								# browser window rectangle
								driver.set_window_rect(x=10, y=10, width=100, height=200)
								

set_window_position

set_window_position method sets the coordinates for the browser starting point, top right corner is considered as (0, 0) position.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								# browser window position
								driver.set_window_position(x=200, y=50)
								
coordinates-for-set-browser-size

get_window_position

Sometimes not only setting a browser size is required, we may need to get the size as well to perform some operations based on the size of the window.

We can use get_window_position() method to retrieve the position of the window. This method returns a dictionay containing x and y.


								# get position of the window
								driver.get_window_position()
								

get_window_size

get_window_size method returns the width and height of the current window, this method returns a dictionary.


								# get size of the window
								driver.get_window_size()
								

WebPage Properties

Webpage properties are nothing but the URL, Page title, page source code, you can use these values for assertion.

Page URL in Selenium python

Page is URL is nothing but the address of the website which is present in the address bar of the browser. Using current_url variable, we can fetch the url of the page.


								# open website
								driver.get("http://google.com")
								print("URL : " + driver.current_url)
								
								

Page title in Selenium python

We can retrieve the webpage title using the title variable present in driver, title is variable not a method.


								driver.get("http://google.com")
								print("Title : " + driver.title)
								

Page title in Selenium python

We can retrieve the page source code using the page_source variable present in the driver, page source is nothing but the html code used to develop the webpage. If they have used javascript ways of replacing the text then you may get only the javascripted html


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								print("Title : " + driver.title)
								print("URL : " + driver.current_url)
								print("Page source : " + driver.page_source)
								

Navigation commands are some which enables the user to navigate through history of the browser like back, forth, refresh. Basically these navigation commands are nothing but the browser navigation buttons, We can use these commands only when these buttons are enabled. Buttons are shown in below image. navigation-selenium-python

back()

back() method navigates the user back to the last page, For example you navigate from Google to bing, and if you use back() method on the bing page, the selenium navigates the page to the google page.

To use back() method you must atleaset moved from one page to another page ateaset once, his method will not work if you have not opened any page but you trying to use back() method.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								print("1. Title is :"+driver.title)
								driver.get("http://bing.com")
								print("2. Title is :"+driver.title)
								# navigate back
								driver.back()
								print("After navigating back Title is :"+driver.title)
								

forward()

forward() help user to navigate forward when already is moved back, using back() method is must before using forward() method otherwise there is no use.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								print("1. Title is :"+driver.title)
								driver.get("http://bing.com")
								print("2. Title is :"+driver.title)
								# navigate back
								driver.back()
								print("After navigating back Title is :"+ driver.title)
								driver.forward()
								print("After moving forward Title is : "+ driver.title)
								

refresh()

refresh method refreshes the current webpage, and loads the same page.


								#CherCherTech
								# import the webdriver
								from selenium import webdriver
								# set exe path and open the browser.
								driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
								# open website
								driver.get("http://google.com")
								print("1. Title is :"+driver.title)
								driver.get("http://bing.com")
								print("2. Title is :"+driver.title)
								# navigate back
								driver.back()
								print("After navigating back Title is :"+ driver.title)
								driver.forward()
								print("After moving forward Title is : "+ driver.title)
								driver.refresh()
								print("After refreshing Title is : "+driver.title)
								
navigate-python-selenium

Different ways to Refresh a webpage

We can refresh a webpage using multiple ways other than refresh() methods, I have listed few here in below.

1. driver.get(driver.current_url)
2. driver.find_element_by_tag_name("body").send_keys(Keys.F5)
3. driver.refresh()
4. driver.find_element_by_tag_name("body").send_keys("\uE035")

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Amol
    I have que for u,
    Can we use both,  if I want to work on Selenium With Java or I want to work on Selenium With Python, On the same Eclipse?
    I am using Eclipse Mars 2...
    
    So plz mail me your suggestion.
    
    
    Reply
    • karthiQ [ admin]
      Hi Amol,
      
      You can use Java and Python in same eclipse but not for same project.
      
      This page shows how to install pydev : http://chercher.tech/python/install-selenium-python
      
      After installtion you just need to change the perspective to PyDev
      
      I also mailed you the screenshot my eclipse which works in python and Java
      Reply
  •  
    Selenium-Webdriver.Com [Group]
    Facebook Group · 801 members
    Join Group
    Learn Selenium Webdriver Concepts and Interview Questions Learn and code
     
    Copyright © CherCher Tech