-
Notifications
You must be signed in to change notification settings - Fork 128
/
screenshot.py
74 lines (62 loc) · 1.98 KB
/
screenshot.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
import argparse
import os
import shutil
import time
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from splinter import Browser
from splinter.driver.webdriver import BaseWebDriver, WebDriverElement
def get_chrome_force_lang(lang):
options = Options()
options.add_experimental_option("prefs", {"intl.accept_languages": lang})
browser = BaseWebDriver()
browser.element_class = WebDriverElement
browser.driver = Chrome(options=options)
return browser
def get_chrome(remote_url, lang=None, width=None, height=None):
# TODO: use `lang`
browser = Browser(
driver_name="remote",
browser="chrome",
command_executor=remote_url,
keep_alive=True,
)
if width is not None and height is not None:
browser.driver.set_window_size(width, height)
return browser
def take_element_screenshot(
selenium_url, url, element_xpath, lang=None, wait=None, width=None, height=None
):
browser = get_chrome(
remote_url=selenium_url,
lang=lang,
width=width,
height=height,
)
browser.visit(url)
if wait:
time.sleep(wait)
element = browser.find_by_xpath(element_xpath).first
filename = element.screenshot(full=True)
browser.quit()
return filename
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--selenium-url", default="http://selenium:4444")
parser.add_argument("filename")
args = parser.parse_args()
url = "https://datastudio.google.com/u/0/reporting/5f774971-ab0f-4b89-af6f-0014332c3e84/page/YbETB"
element_xpath = "//div[@class='reportArea']"
width = 1600
height = 1600
filename = take_element_screenshot(
selenium_url=args.selenium_url,
url=url,
element_xpath=element_xpath,
lang="pt-BR",
wait=5,
width=width,
height=height,
)
shutil.move(filename, args.filename)
os.chmod(args.filename, 0o666)