Ultimate Guide to WebDriver Manager: Boost Your Automation Testing
If you’ve ever faced difficulties managing browser drivers for Selenium WebDriver, you know the pain of version mismatches and manual downloads. Thankfully, `webdriver-manager` simplifies this process, ensuring you always have the right driver for your tests. This guide provides an in-depth look at webdriver-manager
and showcases numerous helpful API examples.
Introduction to WebDriver Manager
WebDriver Manager is a library that automates the management of binary drivers required by Selenium WebDriver. Whether you are working with Chrome, Firefox, Edge, or other browsers, WebDriver Manager fetches the appropriate driver version and sets the necessary environment variables, making your Selenium setup more reliable and less error-prone.
Installing WebDriver Manager
pip install webdriver-manager
Basic Usage
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get("http://www.example.com")
In this example, the ChromeDriverManager
class fetches the latest ChromeDriver, installs it, and passes it to the webdriver.Chrome
instance.
Browser Compatibility
from webdriver_manager.firefox import GeckoDriverManager from webdriver_manager.microsoft import EdgeChromiumDriverManager driver_firefox = webdriver.Firefox(executable_path=GeckoDriverManager().install()) driver_firefox.get("http://www.example.com") driver_edge = webdriver.Edge(EdgeChromiumDriverManager().install()) driver_edge.get("http://www.example.com")
With just a few lines of code, you can manage Firefox and Edge drivers effortlessly.
Specifying Driver Versions
driver_chrome = webdriver.Chrome(ChromeDriverManager(version="91.0.4472.19").install()) driver_chrome.get("http://www.example.com")
You can also specify a particular version of the driver you need for compatibility with older browser versions.
Using with Selenium Grid
from webdriver_manager.chrome import ChromeDriverManager from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService chrome_service = ChromeService(ChromeDriverManager().install()) driver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.CHROME, options=None, service_args=None, service=chrome_service ) driver.get("http://www.example.com")
An Example App
Let’s see how all these pieces come together in a simple web scraping application that captures the titles of the latest blog posts from a website.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager def get_latest_blog_titles(url): driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get(url) titles = driver.find_elements_by_css_selector(".blog-post-title") for title in titles: print(title.text) driver.quit() if __name__ == "__main__": get_latest_blog_titles("http://www.exampleblog.com")
In this example, the get_latest_blog_titles
function navigates to a blog, extracts the titles of the latest posts, and prints them.
With WebDriver Manager, managing browser drivers has never been easier, freeing you to focus more on writing robust tests and applications.
Hash: cc18d576a6c0266c51540483d8034bfdf10752232b2dcf028166dd60176e5394