Load Webdrivers based user on input selenium Python

In this post, we are going to learn how to Load Webdrivers based on user input in selenium Python. Sometimes it needs to load the different web drivers based on the name entered by the user and open a different web driver.

1. Load Web Drivers using web driver-manager

In the manual process, we have to download the browser driver, unzipped, and copied it to the Python script directory. But it is time-consuming, We can automate this work just simply using The webdriver-manager . The first step is to Install the web driver manager by using the command. Let’s understand how to use it in our code to launch Microsoft Edge browsers. We will use different browsers’ web drivers.

pip install webdriver-manager

2. Load WebdrLoad Webdrivers based user on input selenium Python

We have to import different web DriverManager APIs into our program to use it and a List of different driver-manager APIs.

  • from webdriver_manager.chrome import ChromeDriverManager : for Chrome
  • from webdriver_manager.microsoft import EdgeChromiumDriverManager : for Microsoft Edge
  • from webdriver_manager.firefox import GeckoDriverManager : For Firefox

In this python program, we are loading different web drivers based on the name entered by the user. To open a tab or navigate to a link in Python Selenium API, get(‘https://www.google.com’) method is used, Whereas open a new tab in a Window Javascript window. open() method is used. We execute the javascript function by Python Selenium API execute_script() method. The web driver supports moving between windows by using switch_to.window() method.

from selenium import webdriver
import logging
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager 
from webdriver_manager.firefox import GeckoDriverManager

# here initialize empty Webdriver
webdriver.driver = None
browserName = input("Please entered browser name(chrome/firefox/edge):")  
if browserName.upper() == "CHROME":
    driver = webdriver.Chrome(ChromeDriverManager().install())
elif browserName.upper() == "FIREFOX":
   driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
elif browserName.upper() == "EDGE":    
      driver = webdriver.Edge(EdgeChromiumDriverManager(log_level=logging.ERROR).install())
    print("No browsername is entered")
# duckduckgo.com is first Tab
# It will open https:// www.chrome.com is Second Tab
# It will open https://www.yahoo.com is Third Tab
driver.execute_script("window.open('about:blank', 'Tab_3');")