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
- How to Install selenium on windows Python
- How to install selenium in VSCode
- How to open chrome browser in selenium Python
- How to open Edge browser in selenium Python
- How to open Firefox browser in selenium Python
- Executable path has been deprecated in selenium
- Solved version of ChromeDriver only supports Chrome version 98
- How to make test suite in selenium Python
- Solved selenium find_element_by_* commands are deprecated
- How to open new tab in Selenium Python
- How to open new window Selenium Python
- How to open close tab in Selenium Python
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())
else:
print("No browsername is entered")
# duckduckgo.com is first Tab
driver.get('http://duckduckgo.com')
# It will open https:// www.chrome.com is Second Tab
driver.execute_script("window.open('about:blank','Tab_2');")
driver.switch_to.window("Tab_2")
driver.get('https://www.chrome.com/')
# It will open https://www.yahoo.com is Third Tab
driver.execute_script("window.open('about:blank', 'Tab_3');")
driver.switch_to.window("Tab_3")
driver.get('https://www.yahoo.com/')
driver.quit()