Using standard and custom browser

In this part, I will show you how to use SELENIUM to start automating commonly used and custom browsers, how to open and close new browsing tabs

Welcome to the second SELENIUM with Python tutorial part. I will show you how to use SELENIUM to automate commonly used and custom browsers and open and close new browsing tabs.

In the previous tutorial, we wrote a simple python script to open the Firefox browser and use google search with few lines of code.

In this tutorial, we'll write 3 new functions: to start a browser, open a new tab, and close the current tab. So let's begin.

We will begin with starting the browser function. So we call this function with the argument "browser=1" when we use our default browser and "browser=2" when we want to use our custom browser. I tried this code with Google Chromium, but you can try whatever you want. Just write the correct path to browser location and browser driver. I commented few lines of code. If you want, you can remove the comment sign from them, but I won't use them in my code. And at the last line, we return "driver" because we'll use this in all our future functions to control a browser. We call StartBrowser() function from the main code when we want to start a browsing session.

def StartBrowser(browser=1, browser_location='', driver_path=''):
    if browser == 1:
        driver = webdriver.Firefox()
    if browser == 2:
        options = webdriver.ChromeOptions()
        options.binary_location = browser_location
        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)
    #driver.implicitly_wait(30)
    #driver.set_window_size(1920,1080)
    #driver.maximize_window()
    return driver

When we are browsing something on the internet, we would like to use new tabs, so we'll write a function to open it. Selenium webdriver doesn't have an integrated command to open new tabs on the internet, so we'll write our own using a script. Before opening the new tab, we are checking how many tabs we have currently opened: len(driver.window_handles). Then we execute our script to open a new tab, and we are waiting for it, by every 500ms checking our tabs count, when our tabs count increase by one, it means that new tab opened and we may switch to it:

def OpenNewTab(driver, Link):
    window_count = len(driver.window_handles)
    driver.execute_script('''window.open("'''+Link+'''","_blank");''')
    while len(driver.window_handles) != window_count+1:
        time.sleep(0.5)
    driver.switch_to.window(driver.window_handles[-1])

And lastly, we would usually we would like to know how to close these tabs. It's quite easy. We will write a new function for that, with a simple command: driver.close(). In this function, we'll handle our first exception, which occurs when we are trying to close not the existing window. For example, when we close one window and we trying to close it again, we get this exception:

def DriverClose(driver):
    try:
        driver.close()
    except NoSuchWindowException:
        print("NoSuchWindowException in driver.close()")
        return False
    return True

Final tutorial code:

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchWindowException

def DriverClose(driver):
    try:
        driver.close()
    except NoSuchWindowException:
        print("NoSuchWindowException in driver.close()")
        return False
    return True

def StartBrowser(browser=1, browser_location='', driver_path=''):
    if browser == 1:
        driver = webdriver.Firefox()
    if browser == 2:
        options = webdriver.ChromeOptions()
        options.binary_location = browser_location
        driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)
    #driver.implicitly_wait(30)
    #driver.set_window_size(1920,1080)
    #driver.maximize_window()
    return driver

def OpenNewTab(driver, Link):
    window_count = len(driver.window_handles)
    driver.execute_script('''window.open("'''+Link+'''","_blank");''')
    while len(driver.window_handles) != window_count+1:
        time.sleep(0.5)
    driver.switch_to.window(driver.window_handles[-1])

browser_location = "C:/Users/HOME/Desktop/chrome-win/chrome.exe"
driver_path = "C:/Users/HOME/Desktop/chrome-win/chromedriver.exe"
driver = StartBrowser(2, browser_location, driver_path)
time.sleep(1)
driver.get("https://www.google.com")
time.sleep(1)
OpenNewTab(driver, "https://www.google.com")

In the above code, we are starting a new browser session with driver = StartBrowser(2, browser_location, driver_path) line. If you would use it with Firefox browser, change it to driver = StartBrowser(1). Then we open the google search page, and in one second, we open the same page in a new tab. If you would like to see how we close these tabs, check my YouTube video tutorial.

That's all for this short tutorial. We'll cover how to handle exceptions and methods to find HTML objects on the page in the next part.