Skip to content

Instantly share code, notes, and snippets.

@BitAndQuark
Created March 7, 2019 08:47
Show Gist options
  • Save BitAndQuark/87a3bd917e00210831a6b2cf2cac2e32 to your computer and use it in GitHub Desktop.
Save BitAndQuark/87a3bd917e00210831a6b2cf2cac2e32 to your computer and use it in GitHub Desktop.
example of using selenium to interact with web page
# webdriver can initialize object that control supported browsers, such as Chrome
from selenium import webdriver
# Keys contain keyboard keys such as left arrow
from selenium.webdriver.common.keys import Keys
# Select can wrap html <select> tag into an object that has powerful methods
from selenium.webdriver.support.ui import Select
# this is a parser for HTML
import lxml.html
# Create driver object
# requires putting Chrome webdrive in path: https://sites.google.com/a/chromium.org/chromedriver/downloads
driver = webdriver.Chrome()
# Go to an address
driver.get('https://www.something.com')
# Find element and select
location_list = Select(driver.find_element_by_name('my_select'))
location_list.select_by_visible_text('my_option')
# Time range elements
start_date = driver.find_element_by_name('TextBox1')
start_time = driver.find_element_by_name('TextBox6')
# Fill in data
start_date.send_keys('03/06/2019')
# Simulate clicking
start_time.click()
# Shift left by 8 times, so won't overlap text
start_time.send_keys(*([Keys.LEFT]*8))
start_time.send_keys('16:00:00')
# Click a checkbox
driver.find_element_by_id('SomeCheckbox').click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment