-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
96 lines (88 loc) · 4.21 KB
/
browser.py
File metadata and controls
96 lines (88 loc) · 4.21 KB
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
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
TIMEOUT = 240
def set_options(download_path):
options = Options()
options.headless = True
options.set_preference("browser.download.dir", download_path)
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.useDownloadDir", True)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.viewableInternally.enabledTypes", "")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv, text/tab-separated-values, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
return options
def set_preferences():
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.startup.homepage", "about:blank")
profile.set_preference("browser.chrome.toolbar_style", 1) # Text on Toolbar instead of icons
profile.set_preference("browser.display.show_image_placeholders", False) # Don't show thumbnails on not loaded images.
profile.set_preference("browser.display.use_document_colors", False) # Don't show document colors.
profile.set_preference("browser.display.use_document_fonts", 0) # Don't load document fonts.
profile.set_preference("browser.display.use_system_colors", True) # Use system colors.
profile.set_preference("browser.formfill.enable", False) # Autofill on forms disabled.
profile.set_preference("browser.shell.checkDefaultBrowser", False)
profile.set_preference("browser.startup.homepage", "about:blank")
profile.set_preference("browser.startup.page", 0) # blank
profile.set_preference("browser.tabs.forceHide", True) # Disable tabs, We won't need that.
profile.set_preference("browser.urlbar.autoFill", False) # Disable autofill on URL bar.
profile.set_preference("browser.urlbar.autocomplete.enabled", False) # Disable autocomplete on URL bar.
profile.set_preference("browser.urlbar.showPopup", False) # Disable list of URLs when typing on URL bar.
profile.set_preference("browser.urlbar.showSearch", False) # Disable search bar.
profile.set_preference("extensions.checkCompatibility", False) # Addon update disabled
profile.set_preference("extensions.checkUpdateSecurity", False)
profile.set_preference("extensions.update.autoUpdateEnabled", False)
profile.set_preference("extensions.update.enabled", False)
profile.set_preference("general.startup.browser", False)
profile.set_preference("plugin.default_plugin_disabled", False)
profile.set_preference("permissions.default.image", 2) # Image load disabled again
profile.update_preferences()
return profile
def login(
url='http://localhost',
download_path='.',
username=[],
password=[],
submit='',
):
driver = webdriver.Firefox(
options=set_options(download_path),
firefox_profile=set_preferences(),
service_log_path=os.devnull
)
wait = WebDriverWait(driver, TIMEOUT)
driver.get(url)
wait.until(EC.visibility_of_element_located((By.ID, username[0])))
driver.find_element_by_id(username[0]).send_keys(username[1])
wait.until(EC.visibility_of_element_located((By.ID, password[0])))
driver.find_element_by_id(password[0]).send_keys(password[1])
driver.find_element_by_id(submit).click()
return driver
def wait_for_id(driver, id):
wait = WebDriverWait(driver, TIMEOUT)
try:
wait.until(EC.visibility_of_element_located((By.ID, id)))
return True
except TimeoutException:
return False
def get_id(driver, id):
try:
return driver.find_element_by_id(id) if wait_for_id(driver, id) else None
except TimeoutException:
return None
def wait_for_class(driver, class_):
wait = WebDriverWait(driver, TIMEOUT)
try:
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, class_)))
return True
except TimeoutException:
return False
def get_class(driver, class_):
try:
return driver.find_elements_by_class_name(class_) if wait_for_class(driver, class_) else None
except TimeoutException:
return None