Simplifying Browser Automation with Helium
Before: Verbose Selenium Code
Traditional Selenium browser automation scripts can be verbose and difficult to maintain, requiring explicit element locators and waits.
Here’s an example of a Selenium script that logs into GitHub, navigates to a repository, and stars and unstars it:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Start Chrome
with webdriver.Chrome() as driver:
# Navigate to GitHub login page
driver.get('https://github.com/login')
# Login
username_field = driver.find_element(By.ID, 'login_field')
password_field = driver.find_element(By.ID, 'password')
username_field.send_keys('1mh')
password_field.send_keys('1Secretpw')
login_button = driver.find_element(By.NAME, 'commit')
login_button.click()
# Navigate to repository
driver.get('https://github.com/mherrmann/helium')
# Wait for and click Star button
wait = WebDriverWait(driver, 10)
star_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Star')]")))
star_button.click()
# Wait for and click Unstar button
unstar_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Unstar')]")))
unstar_button.click()
After: Simplified Helium Code
Helium provides several benefits over traditional Selenium scripts:
Helium’s APIs simplify code, making it concise and readable by eliminating explicit element locators and waits.
Helium scripts are more maintainable, as you can focus on logic without worrying about implementation details.
Helium’s intuitive API enables faster development, allowing you to write browser automation scripts quickly and save time.
With Helium, the same script becomes much simpler and more intuitive:
from helium import *
# Start Chrome and navigate to GitHub login page
start_chrome('github.com/login')
# Enter username and password
write('1mh', into='Username')
write('1Secretpw', into='Password')
# Click the Sign in button
click('Sign in')
# Navigate to the Helium repository
go_to('github.com/mherrmann/helium')
# Star and then unstar the repository
click(Button('Star'))
click(Button('Unstar'))
# Close the browser
kill_browser()
Link to Helium.
Favorite
Simplifying Browser Automation with Helium Read More »