adding projects to repo

This commit is contained in:
dtomlinson
2019-11-04 14:38:31 +00:00
parent 6728b1ecf3
commit a285fad1ee
65 changed files with 171244 additions and 1 deletions

BIN
splunk-dashboard-screenshot/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,19 @@
# Selenium
## Move the mouse to element(s) then click at the end
```python
e1 = driver.find_element_by_xpath('//*[@id="nav"]/ol/li[5]/a')
e2 = e1.find_element_by_xpath('../ul/li[1]/a')
actions.move_to_element(e1).move_to_element(e2).perform()
e2.click()
```
https://stackoverflow.com/questions/43161987/python-selenium-how-to-move-mouse-on-element-which-shows-drop-down-menu
https://stackoverflow.com/questions/920910/sending-multipart-html-emails-which-contain-embedded-images
class this all up!
add image to a class and iterate through adding each one

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 KiB

View File

@@ -0,0 +1,72 @@
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from selenium.common.exceptions import NoSuchElementException
# from PIL import Image
URL = 'https://sc1uxpremn81:8000/en-US/app/wh_netacea_blocking/byo_decoder_wip'
URL2 = (
'https://sc1uxpremn81:8000/en-US/app/wh_netacea_blocking/'
'blocking_visibility?form.time_tok.earliest=%40d&form.time_tok.latest=now'
)
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get(URL2)
driver.implicitly_wait(10)
driver.maximize_window()
action = ActionChains(driver)
username = driver.find_element_by_xpath('//*[@id="username"]')
password = driver.find_element_by_xpath('//*[@id="password"]')
username.send_keys('admin')
password.send_keys('fWgbz6AU')
driver.find_element_by_xpath(
'/html/body/div[2]/div/div/div[1]/form/fieldset/input[1]'
).click()
def setWidth(var, adj=0):
script = "return document.body.parentNode.scroll" + (var)
return driver.execute_script(script) + adj
driver.set_window_size(setWidth('Width', 1000), setWidth('Height', 10000))
sleep(2)
try:
driver.find_element_by_xpath(
'/html/body/div[2]/div/div[3]/div[1]/a'
).click()
except NoSuchElementException:
pass
try:
element = driver.find_element_by_class_name('dashboard-menu')
driver.execute_script("arguments[0].style.visibility = 'hidden';", element)
except NoSuchElementException:
pass
try:
action.move_to_element(
driver.find_element_by_class_name('dashboard-title')
).perform()
sleep(1)
except NoSuchElementException:
pass
driver.find_element_by_xpath('/html/body/div[2]').screenshot(
'web_screenshot.png'
)
driver.quit()

View File

@@ -0,0 +1,137 @@
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotInteractableException
from time import sleep
import os
class getDashboardScreenshot(object):
def __init__(
self,
driver: webdriver,
url: str,
username: str,
password: str,
width: int = 0,
height: int = 10000,
path: str = os.getcwd(),
delay: int = 1,
):
super(getDashboardScreenshot, self).__init__()
self.driver = driver
self.action = ActionChains(driver)
self.url = url
self.username = username
self.password = password
self.width = width
self.height = height
self.path = self.checkPath(path)
self.delay = delay
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
self.driver.quit()
@classmethod
def createDriver(
cls,
url: str,
username: str,
password: str,
width: int = 0,
height: int = 0,
type: str = 'chromium',
path: str = os.getcwd(),
delay: int = 1,
):
if type == 'chromium':
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
else:
print(f'No supported browser {type}')
return cls(driver, url, username, password, width, height, path, delay)
@staticmethod
def checkPath(path: str) -> str:
if path[-1] != '/':
path += '/'
return path
def _setWidth(self, jsProperty: str, override: int):
script = 'return document.body.parentNode.scroll' + (jsProperty)
return self.driver.execute_script(script) + override
def _logIn(self):
self.usernameElement = self.driver.find_element_by_xpath(
'//*[@id="username"]'
)
self.passwordElement = self.driver.find_element_by_xpath(
'//*[@id="password"]'
)
self.usernameElement.send_keys(self.username)
self.passwordElement.send_keys(self.password)
self.driver.find_element_by_xpath(
'/html/body/div[2]/div/div/div[1]/form/fieldset/input[1]'
).click()
sleep(2)
return self
def _hideElements(self):
try:
self.driver.find_element_by_class_name(
'hide-global-filters'
).click()
except (NoSuchElementException, ElementNotInteractableException):
pass
try:
element = self.driver.find_element_by_class_name('dashboard-menu')
self.driver.execute_script(
"arguments[0].style.visibility = 'hidden';", element
)
except NoSuchElementException:
pass
try:
self.action.move_to_element(
self.driver.find_element_by_class_name('dashboard-title')
).perform()
sleep(1)
except NoSuchElementException:
pass
return self
def _goToDashboard(self):
self.driver.get(self.url)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self._logIn()
return self
def takeScreenshot(self):
self._goToDashboard()
self.driver.set_window_size(
self._setWidth('Width', self.width),
self._setWidth('Height', self.height),
)
self._hideElements()
sleep(self.delay)
self.driver.find_element_by_class_name('dashboard-body').screenshot(
f'{self.path}web_screenshot.png'
)
return self
with getDashboardScreenshot.createDriver(
url='https://sc1uxpremn81:8000/en-US/app/wh_weekly_standup/weekly_standup_'
'data?form.time_tok.earliest=-7d%40h&form.time_tok.latest=now&hideFilters='
'true',
username='admin',
password='fWgbz6AU',
width=1000,
height=10000,
delay=5,
) as screenShot:
screenShot.takeScreenshot()

View File

@@ -0,0 +1,6 @@
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.python.org')
driver.save_screenshot('screenshot.png')
driver.quit()

View File

@@ -0,0 +1,32 @@
# https://stackoverflow.com/questions/41721734/take-screenshot-of-full-page-with-selenium-python-with-chromedriver/57338909#57338909
from selenium import webdriver
from PIL import Image
URL = 'https://www.trafficdelays.co.uk/m62-traffic-delays/'
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get(URL)
def setWidth(var, adj=0):
script = "return document.body.parentNode.scroll" + (var)
return driver.execute_script(script) + adj
driver.set_window_size(setWidth('Width'), setWidth('Height'))
driver.find_element_by_tag_name('body').screenshot('web_screenshot.png')
# driver.find_element_by_css_selector('#post-4706').screenshot('web_screenshot.png')
# print(driver.find_element_by_css_selector('#post-4706').text)
im = Image.open('web_screenshot.png')
width, height = im.size
region = im.crop((0, 0, width, 880))
region.save('cropped.png')
driver.quit()