Getting Started with Selenium in Python: A Comprehensive Guide
Ohidur Rahman Bappy
MAR 22, 2025
Getting Started with Selenium in Python: A Comprehensive Guide
Introduction
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. In this guide, we'll explore the basic usage of Selenium with Python.
Importing Selenium Libraries
First, you need to import the essential Selenium libraries.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
Setting Up WebDriver
For browser automation, you'll need a WebDriver for the browser you wish to automate.
Chrome
Download ChromeDriver here.
chromedriver = "C:/tests/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver)
Firefox
Get GeckoDriver here.
geckodriver = "C:/tests/geckodriver.exe"
driver = webdriver.Firefox(executable_path=geckodriver)
Internet Explorer
IEDriverServer is available here.
iedriver = "C:/tests/IEDriverServer.exe"
driver = webdriver.Ie(executable_path=iedriver)
Safari
No download needed. SafariDriver is built into Safari.
driver = webdriver.Safari()
Using Endtest for Browser Automation
Endtest is a platform for Codeless Automated Testing on real browsers and mobile devices.
Browser Options and Arguments
Here are some common browser arguments which can enhance your testing setup:
- --headless: Runs browser in headless mode (no GUI).
- --start-maximized: Starts Chrome maximized.
- --incognito: Opens Chrome in incognito mode.
- --disable-notifications: Disables Chrome notifications.
Example setup:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--start-maximized")
options.add_argument("--disable-notifications")
options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=options, executable_path="path to driver")
Browser Details and Navigation
Retrieve Browser Information
driver.title
driver.window_handles
driver.current_window_handle
driver.current_url
driver.page_source
Navigation Commands
driver.get(url)
driver.back()
driver.forward()
driver.refresh()
Interacting with Web Elements
Opening a Website
the_url = "https://example.com"
driver.get(the_url)
Locating Elements
You can find elements using various strategies:
-
By ID
element = driver.find_element_by_id('register')
-
By Name
element = driver.find_element_by_name('register')
-
By Class Name
element = driver.find_element_by_class_name('nav-link')
-
By Tag Name
element = driver.find_element_by_tag_name('a')
-
By Link Text
element = driver.find_element_by_link_text('Sign Up')
-
By Partial Link Text
element = driver.find_element_by_partial_link_text('Sign')
-
By CSS Selector
element = driver.find_element_by_css_selector('a[href="/sign-up"]')
-
By XPath
element = driver.find_element_by_xpath('//a[@href = "/sign-up"]')
Interacting with Elements
Click on Element
element.click()
Send Keys to Input
element.send_keys('klaus@werner.de')
Select Options from Dropdown
Using visible text:
select_element = Select(driver.find_element_by_id('country'))
select_element.select_by_visible_text('Canada')
Using value:
select_element.select_by_value('CA')
Using index:
select_element.select_by_index(1)
Taking Screenshots
driver.save_screenshot('C:/tests/screenshots/1.png')
Uploading Files
element.send_keys('C:/tests/files/example.pdf')
Execute JavaScript
driver.execute_script('document.getElementById("pop-up").remove()')
Handling iFrames
driver.switch_to.frame(driver.find_element_by_id('payment_section'))
element.send_keys('41111111111111')
driver.switch_to.default_content()
Tab and Window Handling
Switching and closing browser tabs programmatically.
More Actions
Right-click, drag and drop, hover, and more with ActionChains
.
Handling Cookies
Getting and Deleting Cookies
cookies_list = driver.get_cookies()
driver.delete_cookie('shopping_cart')
driver.delete_all_cookies()
Managing Waits
Implicit Waits
driver.implicitly_wait(10)
Explicit Waits
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.NAME, "q"))
)
element.send_keys("selenium")
element.send_keys(Keys.ENTER)
except TimeoutException:
print("Failed to load search bar at www.google.com")
Additional Techniques
Changing Browser Settings
Setting user agent, simulating webcam and microphone, and using extensions.
Mobile Emulation
options = webdriver.ChromeOptions()
pixel_3_xl_emulation = {
"deviceMetrics": {
"width": 411,
"height": 731,
"pixelRatio": 3
},
"userAgent": 'Mozilla/5.0 (Linux; Android 9.0; Pixel 3 XL ...)'}
options.add_experimental_option("mobileEmulation", pixel_3_xl_emulation)
Conclusion
Selenium is a versatile tool that can automate the repetitive tasks in browsers, intended for both testing and scraping applications. Mastering these foundational skills will equip you to tackle a variety of web automation tasks.