Skip to content

Instantly share code, notes, and snippets.

@tinoji
Last active October 31, 2021 19:22
Show Gist options
  • Save tinoji/465932f64f1453a612c19be0a4caccd4 to your computer and use it in GitHub Desktop.
Save tinoji/465932f64f1453a612c19be0a4caccd4 to your computer and use it in GitHub Desktop.
Take screenshot and send it to Slack by Selenium
# coding: utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import requests
import json
import os
import time
BASEDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "screenshots/")
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver", options=options)
driver.set_window_size(1280, 920)
driver.get("<login url>")
time.sleep(<time>)
# login example
login = driver.find_element_by_name("<element name of user name input>")
login.send_keys("<user name>")
password = driver.find_element_by_name("<element name of password input>")
password.send_keys("<password>")
login_button = driver.find_element_by_name("commit")
login_button.click()
# screen shot
driver.get("<url>")
time.sleep(<time>)
driver.save_screenshot(BASEDIR + "<file name>.png")
driver.quit()
# send to slack
TOKEN = "<token>"
CHANNEL = "<channed id>"
files = {'file': open(BASEDIR + "<file name>.png", 'rb')}
param = {
'token':TOKEN,
'channels':CHANNEL,
'filename':"<filename to send>.png",
'initial_comment': "<comment>",
'title': "<title>"
}
requests.post(url="https://slack.com/api/files.upload",params=param, files=files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment