-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_handler.py
executable file
·86 lines (72 loc) · 3.06 KB
/
browser_handler.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
import datetime
import time
from selenium.common.exceptions import WebDriverException
from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.firefox.options import Options
from xvfbwrapper import Xvfb
TIMESTAMP = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S')
class BrowserHandler:
def __init__(self, args):
self.args = args
if self.args and not self.args.show_browser:
self.display = Xvfb()
self.display.start()
log_level = self._define_log_level(self.args)
capabilities = self._create_browser_capabilities(log_level)
options = self._create_browser_options(log_level)
profile = self._create_browser_profile(self.args.destination)
self.browser = Firefox(
firefox_profile=profile,
capabilities=capabilities,
options=options,
log_path="{timestamp}_geckodriver.log".format(timestamp=TIMESTAMP)
)
# http://stackoverflow.com/questions/42754877/cant-upload-file-using-selenium-with-python-post-post-session-b90ee4c1-ef51-4 # pylint: disable=line-too-long
self.browser._is_remote = False # pylint: disable=protected-access
@staticmethod
def _define_log_level(args):
if args and args.verbose and args.verbose >= 3:
log_level = 'trace'
elif args and args.verbose and args.verbose == 2:
log_level = 'debug'
elif args and args.verbose and args.verbose == 1:
log_level = 'info'
else:
log_level = 'warn'
return log_level
@staticmethod
def _create_browser_capabilities(log_level):
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities["moz:firefoxOptions"] = {
"log": {
"level": log_level,
},
}
return capabilities
@staticmethod
def _create_browser_options(log_level):
options = Options()
options.log.level = log_level
return options
@staticmethod
def _create_browser_profile(exports_folder):
profile = FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", exports_folder)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv, application/zip")
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("devtools.jsonview.enabled", False)
profile.set_preference("media.volume_scale", "0.0")
# https://github.com/mozilla/geckodriver/issues/858#issuecomment-322512336
profile.set_preference("dom.file.createInChild", True)
return profile
def kill(self):
self.browser.stop_client()
self.browser.close()
try:
self.browser.quit()
except WebDriverException:
pass
if self.args and not self.args.show_browser:
self.display.stop()