Setting up Selenium with Python is straightforward, and you can use the latest versions of Selenium and Python.
Here’s a step-by-step guide to getting started:
Install Python:
Download and install Python from the official website: Python Downloads. (Download Python | Python.org).
During installation, make sure to check the box that says “Add Python to PATH.”
Install Selenium:
Open a command prompt (Windows) or terminal (macOS/Linux).
Install the Selenium package using pip, Python’s package installer. Run the following command:
pip install selenium
Download a WebDriver:
Selenium requires a WebDriver to interface with different browsers. You can download the WebDriver for your preferred browser:
- Chrome: ChromeDriver
- Firefox: GeckoDriver
- Edge: Microsoft Edge Driver
- Safari: SafariDriver is included in Safari’s Developer Tools. Enable it from Safari > Preferences > Advanced > Show Develop menu in menu bar.
Set Up the WebDriver:
After downloading, extract the WebDriver executable (e.g., chromedriver.exe for Chrome) and place it in a location accessible from your PATH environment variable. Alternatively, you can specify the path to the WebDriver executable in your script.
Write Your First Selenium Script:
Here’s a simple example to open a browser, navigate to a webpage, and get its title using Python and Selenium:
from selenium import webdriver
# Path to the WebDriver executable
driver = webdriver.Chrome(executable_path='path_to_chromedriver.exe')
# Open a website
driver.get('https://www.example.com')
# Get the title of the page
print(driver.title)
# Close the browser
driver.quit()
Run Your Script:
Save the above script to a file with a .py extension (e.g., selenium_script.py).
Open a command prompt or terminal.
Navigate to the directory where your script is saved.
Run the script using Python:
python selenium_script.py
This setup should work with the latest versions of Selenium and Python. If you encounter any issues, make sure to check the Selenium and WebDriver documentation for any updates or changes.