-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
143 lines (115 loc) · 5.7 KB
/
util.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
References
Full page screenshot: https://stackoverflow.com/questions/41721734/take-screenshot-of-full-page-with-selenium-python-with-chromedriver/57338909#57338909
selenium_wait_for_images_loaded.py: https://gist.github.com/munro/7f81bd1657499866f7c2
"""
import os
import time
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
from textwrap import dedent
from datetime import datetime
def fullpage_screenshot(driver, file, debugon=False):
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Starting chrome full page screenshot workaround ...")
total_width = driver.execute_script("return document.body.offsetWidth")
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_width = driver.execute_script("return document.body.clientWidth")
viewport_height = driver.execute_script("return window.innerHeight")
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Total: ({0}, {1}), Viewport: ({2},{3})".format(total_width, total_height,viewport_width,viewport_height))
rectangles = []
i = 0
while i < total_height:
ii = 0
top_height = i + viewport_height
if top_height > total_height:
top_height = total_height
while ii < total_width:
top_width = ii + viewport_width
if top_width > total_width:
top_width = total_width
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Appending rectangle ({0},{1},{2},{3})".format(ii, i, top_width, top_height))
rectangles.append((ii, i, top_width,top_height))
ii = ii + viewport_width
i = i + viewport_height
stitched_image = Image.new('RGB', (total_width, total_height))
previous = None
part = 0
for rectangle in rectangles:
if not previous is None:
lenOfPage = driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1])+";var lenOfPage=document.body.scrollHeight;return lenOfPage;")
match=False
while(match==False):
lastCount = lenOfPage
time.sleep(3)
lenOfPage = driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1])+";var lenOfPage=document.body.scrollHeight;return lenOfPage;")
if lastCount==lenOfPage:
match=True
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Scrolled To ({0},{1})".format(rectangle[0], rectangle[1]))
time.sleep(0.2)
file_name = "part_{0}.png".format(part)
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Capturing {0} ...".format(file_name))
driver.get_screenshot_as_file(file_name)
screenshot = Image.open(file_name)
if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], total_height - viewport_height)
else:
offset = (rectangle[0], rectangle[1])
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Adding to stitched image with offset ({0}, {1})".format(offset[0],offset[1]))
stitched_image.paste(screenshot, offset)
del screenshot
os.remove(file_name)
part = part + 1
previous = rectangle
stitched_image.save(file)
if debugon:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + "[DEBUG] " + "Finishing chrome full page screenshot workaround ...")
return True
def wait_until_images_loaded(driver, timeout=30):
""" Waits for all images & background images to load """
driver.set_script_timeout(timeout)
driver.execute_async_script(dedent('''
function extractCSSURL(text) {
var url_str = text.replace(/.*url\((.*)\).*/, '$1');
if (url_str[0] === '"') {
return JSON.parse(url_str);
}
if (url_str[0] === "'") {
return JSON.parse(
url_str
.replace(/'/g, '__DOUBLE__QUOTE__HERE__')
.replace(/"/g, "'")
.replace(/__DOUBLE__QUOTE__HERE__/g, '"')
);
}
return url_str;
}
function imageResolved(url) {
return new $.Deferred(function (d) {
var img = new Image();
img.onload = img.onload = function () {
d.resolve(url);
};
img.src = url;
if (img.complete) {
d.resolve(url);
}
}).promise();
}
var callback = arguments[arguments.length - 1];
$.when.apply($, [].concat(
$('img[src]')
.map(function (elem) { return $(this).attr('src'); })
.toArray(),
$('[style*="url("]')
.map(function () { return extractCSSURL($(this).attr('style')); })
.toArray()
.map(function (url) { return imageResolved(url); })
)).then(function () { callback(arguments); });
return undefined;
'''))