Launching Different Browsers using Selenium Python
Selenium is one of the most widely used tools for web automation and browser testing. It allows developers and testers to simulate user interactions with web pages across different browsers, making cross-browser testing more accessible and efficient. In this blog post, we'll explore how to launch different browsers—such as Chrome, Firefox, Edge, and Safari—using Selenium with Python.
Why Launch Multiple Browsers?
Web applications often behave differently depending on the browser and its version. Cross-browser testing helps ensure a consistent user experience across:
- Google Chrome (Chromium-based)
- Mozilla Firefox
- Microsoft Edge
- Apple Safari (macOS only)
- Headless browsers (for CI/CD pipelines)
Selenium WebDriver provides browser-specific drivers to control the browser directly. Let's look at how to configure and launch each browser using Python.
Prerequisites
To follow along, make sure you have the following installed:
- Python (3.6+)
- Selenium (pip install selenium)
- WebDriver executables (e.g., chromedriver, geckodriver, etc.) added to your system path
Install Selenium using pip:
bash
pip install selenium
1. Launching Google Chrome
python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
Make sure you have the correct version of ChromeDriver that matches your installed Chrome browser. You can also use webdriver-manager to avoid manual driver setup:
bash
pip install webdriver-manager
python
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.google.com")
2. Launching Mozilla Firefox
python
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.mozilla.org")
Firefox is fully supported and a good choice for testing open web standards.
3. Launching Microsoft Edge
Microsoft Edge (Chromium-based) uses msedgedriver:
python
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager
driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.get("https://www.microsoft.com")
Edge is now Chromium-based, so it behaves similarly to Chrome in many cases.
4. Launching Safari (macOS Only)
Safari comes pre-installed on macOS and has a built-in WebDriver called safaridriver.
python
from selenium import webdriver
driver = webdriver.Safari()
driver.get("https://www.apple.com")
To enable safaridriver, run this command once:
bash
safaridriver --enable
Safari is ideal for macOS/iOS compatibility testing.
5. Running Browsers in Headless Mode
Headless mode allows browsers to run without a GUI—useful for automated testing and CI environments.
Chrome Headless:
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get("https://example.com")
Firefox Headless:
python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
options = Options()
options.headless = True
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)
driver.get("https://example.com")
Conclusion
Launching different browsers using Selenium with Python is a crucial skill for cross-browser and automated testing. Selenium makes it easy to write once and test across multiple platforms. Tools like webdriver-manager simplify driver setup, while headless mode is ideal for server-based environments. By mastering browser launches with Selenium, you ensure your web applications work seamlessly for all users—no matter their browser of choice.
Learn Selenium with Python Training Course
Read More: Setting Up Your First Selenium Python Script
Visit Quality Thought Training Institute in Hyderabad
Get Direction
Comments
Post a Comment