Last active
March 8, 2021 23:53
-
-
Save davidair/46ed266bcdc8d4e071d9a802f11ff177 to your computer and use it in GitHub Desktop.
Selenium-based harness that dynamically evaluates code to allow exploring web automation without re-running the driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2021 Google LLC. | |
# SPDX-License-Identifier: Apache-2.0 | |
search_input = driver.find_element_by_xpath("//input[@id='searchInput']") | |
search_input.clear() | |
search_input.send_keys('George Washington') | |
submit_button = driver.find_element_by_xpath("//button[@type='submit']") | |
submit_button.click() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2021 Google LLC. | |
# SPDX-License-Identifier: Apache-2.0 | |
import pathlib | |
import time | |
from selenium import webdriver | |
# Keep track of the dynamic file's last modified time. | |
last_modified_time = 0 | |
# Initialize the Chrome driver - needs to be in PATH. | |
# To download the driver, visit https://chromedriver.chromium.org/downloads | |
driver = webdriver.Chrome() | |
# Navigate to Wikipedia | |
driver.get('https://www.wikipedia.org/') | |
while True: | |
# Sleep for 100ms to avoid pegging the CPU | |
time.sleep(100 / 1000) | |
# Check for the presence of the file containing dynamic code. | |
fname = pathlib.Path('dyncode_wiki.py') | |
if not fname.exists(): | |
continue | |
modified_time = fname.stat().st_mtime | |
if last_modified_time != modified_time: | |
# The heuristic assumes that if the file's modified time has changed, | |
# it should be re-executed. Note this will always execute at least once | |
# since last_modified_time is initialized to 0. If this is not a desirable | |
# behavior, add a check for that: "and last_modified_time != 0". | |
# Alternatively, a more sophisticated approach would rely on an md5 hash | |
# of the file to only re-run changes if the contents actually changed. | |
print('dyncode changed, evaluating...') | |
last_modified_time = modified_time | |
with open('dyncode_wiki.py') as f: | |
try: | |
exec(f.read()) | |
except Exception as e: | |
print('Error evaluation code: %s' % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment