-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
87 lines (67 loc) · 2.2 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import urllib
import os
default_width = 1024
default_height= 768
enabled = {
'firefox': True,
'chrome': True
}
sizes = [
[320, 240],
[640, 480],
[1024, 768],
[1440, 900],
[1920, 1080],
[2560, 1440]
]
def ensure_dir(file_path):
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
# Website url
# ex: https://leapsandrebounds.com
base_url = "https://leapsandrebounds.com"
hostname = urllib.parse.urlparse(base_url).hostname
# Add url extensions here
# ex: /products/jump-pack
url_extensions = [
"",
"/products/jump-pack"
]
if enabled['firefox']:
firefox = webdriver.Firefox()
for dimensions in sizes:
firefox.set_window_size(dimensions[0], dimensions[1])
for extension in url_extensions:
firefox.get(base_url + extension)
dir = "./screenshots/firefox/"
ensure_dir(dir)
file_name = dir + hostname + extension.replace("/", ".") + "_" + str(dimensions[0]) + "x" + str(dimensions[1]) + ".png"
print("Screenshoting:", file_name)
element=firefox.find_element_by_tag_name('body')
element_png = element.screenshot_as_png
with open(file_name, "wb") as file:
file.write(element_png)
firefox.close()
if enabled['chrome']:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--hide-scrollbars')
chrome = webdriver.Chrome(chrome_options=chrome_options)
for dimensions in sizes:
for extension in url_extensions:
# Get body height
chrome.set_window_size(dimensions[0], dimensions[1])
chrome.get(base_url + extension)
page_height = chrome.find_element_by_tag_name('body').size['height']
chrome.set_window_size(dimensions[0], page_height)
dir = "./screenshots/chrome/"
ensure_dir(dir)
file_name = dir + hostname + extension.replace("/", ".") + "_" + str(dimensions[0]) + ".png"
print("Screenshoting:", file_name)
chrome.get_screenshot_as_file(file_name)
chrome.close()