0
0
Selenium-pythonHow-ToBeginner ยท 3 min read

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 with pip 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.webdriver alone does not import the webdriver module directly; use from 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 webdriver to 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.