Maximize or Minimize a browser window using Selenium Python

Hello programmers, in this tutorial we will see how to maximize or minimize a browser window using Selenium Python.

Selenium is a tool that is used in the automation of browsers. This automation task can be done via various Python, PHP, JavaScript programs, etc.

Here, we will see how to do this task using Python.

Installation:

pip install selenium

In any web browser, the maximize button is used to enlarge a web browser window whereas the minimize button is used to shrink the window.

Before running the code, we have to download the chrome driver and place the file path of the executable file in our system path.

Given below is an illustration for maximizing a window using Selenium Python.

#Importing necessary Libraries
from selenium import webdriver
import time

#maximize function
def seleniumMaximize():
    #creating a webdriver object
    driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
    driver.maximize_window() #maximize window size
    driver.get("https://www.codespeedy.com/author/varunbhattacharya/") #opening the url
    time.sleep(10) #waiting for 10 seconds

#driver
if __name__ == "__main__":
    seleniumMaximize() #call the function

Output

The browser opens with the maximized window.

Explanation
We create a web driver instance of the chrome driver. We then maximize the window using the ‘maximize_window()’ method and open the specified URL.

Given below is an illustration for minimizing a window using Selenium Python.

#Importing necessary Libraries
from selenium import webdriver
import time

#minimize function
def seleniumMinimize():
    #creating a webdriver object
    driver = webdriver.Chrome(executable_path='C:/path/to/dir/chromedriver.exe')
    driver.minimize_window() #minimize window size
    driver.get("https://www.codespeedy.com/author/varunbhattacharya/") #opening the url
    time.sleep(10) #waiting for 10 seconds

#driver
if __name__ == "__main__":
    seleniumMinimize() #call the function

Output

The window is shrunk to the taskbar and the following URL is opened in the browser.

Explanation
We create a web driver instance of the chrome driver. We then minimize the window by using the ‘minimize_window()’ method and open the following URL specified.

Leave a Reply

Your email address will not be published.