How to Import Selenium in Python: Simple Guide
To import Selenium in Python, use
from selenium import webdriver. This imports the WebDriver module, which lets you control browsers for automation tasks.Syntax
The basic syntax to import Selenium's WebDriver in Python is from selenium import webdriver. Here, selenium is the package name, and webdriver is the module that controls browsers.
You can also import specific browser drivers like Chrome or Firefox from selenium.webdriver.
python
from selenium import webdriver
Example
This example shows how to import Selenium and open a Chrome browser window using WebDriver.
python
from selenium import webdriver # Create a new Chrome browser instance browser = webdriver.Chrome() # Open a website browser.get('https://www.example.com') # Close the browser browser.quit()
Output
A Chrome browser window opens and navigates to https://www.example.com, then closes.
Common Pitfalls
- Not installing Selenium before importing it causes
ModuleNotFoundError. Install withpip install selenium. - For browser drivers like ChromeDriver, the driver executable must be installed and in your system PATH or specified in code.
- Using
import selenium.webdriveralone does not import thewebdrivermodule directly; usefrom selenium import webdriver.
python
## Wrong way (does not import webdriver directly): import selenium.webdriver ## Right way: from selenium import webdriver
Quick Reference
Remember these key points when importing Selenium in Python:
- Use
from selenium import webdriverto access browser automation. - Install Selenium first with
pip install selenium. - Ensure browser drivers like ChromeDriver are installed and configured.
Key Takeaways
Always install Selenium using pip before importing it in Python.
Use 'from selenium import webdriver' to access browser automation features.
Make sure the browser driver executable (like ChromeDriver) is installed and in your system PATH.
Avoid using 'import selenium.webdriver' alone as it does not import the webdriver module directly.