Master Selenium WebDriver with Python: Path Setup, Screenshots, Refresh, and JavaScript Execution
This tutorial walks through essential Selenium WebDriver techniques in Python, covering driver path configuration, capturing full and partial screenshots, page refresh methods, opening new tabs, cropping images with Pillow, executing JavaScript, and retrieving script results, all with clear code examples.
Setting the Selenium WebDriver Path
Download the appropriate browser driver (e.g., geckodriver for Firefox) from the official site and place it where the test machine can access it. Instead of hard‑coding absolute paths, keep the driver executable in the system's PATH or the same directory as the browser to avoid path‑related errors.
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
ff_binary = FirefoxBinary('path/to/gecko driver')
browser = webdriver.Firefox(firefox_binary=ff_binary)When the driver resides alongside the browser, Selenium can locate it automatically, reducing configuration mistakes.
Capturing Screenshots
Selenium provides three APIs for screenshots:
save_screenshot('path/filename.png') get_screenshot_as_file('path/filename.png') get_screenshot_as_png()The first two save a PNG file and return True on success, False on IOError. get_screenshot_as_png() returns the image data as a binary string for further processing.
from selenium import webdriver
import StringIO
from PIL import Image
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
driver.save_screenshot('screenshot_1.png')
driver.get_screenshot_as_file('screenshot_2.png')
screenshot = driver.get_screenshot_as_png()
# Example of cropping a region (left, top, right, bottom)
region = (20, 10, 480, 600)
image = Image.open(StringIO.StringIO(screenshot))
crop = image.crop(region)
crop.save('screenshot_3.jpg', 'JPEG', optimize=True)Refreshing a Page
Two common ways to refresh a page during a test:
driver.refresh()
Simply calls the browser's refresh command; it can be combined with document.readyState checks for reliability.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
driver.refresh()ActionChains with CTRL+F5
Uses low‑level keyboard actions to perform a hard refresh.
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.example.com/")
time.sleep(5)
ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.F5).key_up(Keys.CONTROL).perform()
time.sleep(5)
driver.quit()Opening a New Tab
Execute JavaScript to open a URL in a new browser tab.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Firefox()
driver.get("http://www.example.com/")
driver.implicitly_wait(10)
# Open new tab
driver.execute_script("window.open('https://www.example.com', 'new tab')")
sleep(5)
driver.quit()Capturing and Cropping a Specific Element
Use the Pillow library to crop a screenshot to a particular element's bounding box.
from selenium import webdriver
from PIL import Image
from io import BytesIO
driver = webdriver.Firefox()
driver.get('http://example.com/')
# Locate element by ID
element = driver.find_element_by_id('hplogo')
location = element.location
size = element.size
png = driver.get_screenshot_as_png()
image = Image.open(BytesIO(png))
left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height']
crop = image.crop((left, top, right, bottom))
crop.save('logo-screenshot.png')Executing JavaScript and Getting Results
Run arbitrary JavaScript with execute_script(). To retrieve a value, prepend the script with return.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://www.example.com")
# Click an element
driver.execute_script("document.getElementsByClassName('home-cta')[0].click()")
# Get a result from JS
result = driver.execute_script("return 0")
print(result)
sleep(10)
driver.close()The article ends with a note that more content will follow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
