Skip to content

Instantly share code, notes, and snippets.

@JonathanTurnock
Created December 22, 2019 21:52
Show Gist options
  • Save JonathanTurnock/abab91c9a6a9b1d8e3247842ea8c5182 to your computer and use it in GitHub Desktop.
Save JonathanTurnock/abab91c9a6a9b1d8e3247842ea8c5182 to your computer and use it in GitHub Desktop.
Python Selenium Json Placeholder Interaction
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
if __name__ == '__main__':
# 1. Optional Options (Useful for development testing)
options = Options()
options.add_argument("--disable-web-security")
options.add_argument("--disable-application-cache")
# 2. Create the Chrome Driver
driver = webdriver.Chrome(options=options)
# 3. Head to the JsonPlaceholder landing page
driver.get("https://jsonplaceholder.typicode.com/")
driver.save_screenshot('2.png')
# 4. Scroll to the run-message element to bring it into view (Optional)
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_css_selector("#run-message"))
actions.perform()
driver.save_screenshot('4.png')
# 5. Interact with the site
driver.find_element_by_css_selector("#run-button").click() # Get the Run Button Element and Click it
time.sleep(1) # Small Sleep for Async request to go out and DOM be updated
driver.save_screenshot('5.png')
# 6. Confirm the success message was present on the site
print("Congrats you've made your first call to JSONPlaceholder! 😃 🎉" == driver.find_element_by_css_selector("#run-message").text)
driver.save_screenshot('6.png')
# 7. Close the driver to kill the chrome instance
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment