-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.py
129 lines (104 loc) Β· 4.12 KB
/
browser.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
import json
import logging
import lxml.html
from datetime import datetime
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from pyvirtualdisplay.smartdisplay import SmartDisplay
# For convenience
visible = EC.visibility_of_element_located
presence = EC.presence_of_element_located
clickable = EC.element_to_be_clickable
class Browser:
def __init__(self, cookies_file='data/cookies.json', virtual_display=True, log_file=None):
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
if log_file:
fh = logging.FileHandler(log_file)
fh.setLevel(logging.INFO)
self.logger.addHandler(fh)
self.cookies_file = cookies_file
if virtual_display:
self.display = SmartDisplay(visible=0, size=(1920, 1080))
self.display.start()
else:
self.display = None
self._reload_driver()
def quit(self):
if hasattr(self, 'driver'):
self.clear_cookies()
self.driver.quit()
def save_cookies(self):
self.logger.info('Saving cookies')
cookies = self.driver.get_cookies()
with open(self.cookies_file, 'w') as f:
json.dump(cookies, f)
def get_cookies(self):
return self.driver.get_cookies()
def load_cookies(self):
# Note: to load cookies for a domain, you must
# first navigate to that domain.
self.logger.info('Loading cookies')
try:
with open(self.cookies_file, 'r') as f:
cookies = json.load(f)
except FileNotFoundError:
return False
for c in cookies:
if isinstance(c.get('expiry'), float):
c['expiry'] = int(c['expiry'])
self.driver.add_cookie(c)
return True
def clear_cookies(self):
self.driver.delete_all_cookies()
def _reload_driver(self, opts=[]):
self.quit()
options = webdriver.ChromeOptions()
for opt in opts:
options.add_argument(opt)
# Necessary for headless on server
options.add_argument('--no-sandbox')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
# Don't ask to save passwords
options.add_experimental_option('prefs', {
'credentials_enable_service': False,
'profile.password_manager_enabled': False
})
self.driver = webdriver.Chrome(chrome_options=options)
self.wait = WebDriverWait(self.driver, 5)
self.actions = ActionChains(self.driver)
@property
def user_agent(self):
return self.driver.execute_script('return navigator.userAgent')
def screenshot(self, fname=None):
fname = fname or 'data/shots/{}.png'.format(datetime.utcnow().timestamp())
if self.display:
img = self.display.waitgrab()
img.save(fname)
else:
self.driver.save_screenshot(fname)
return fname
def visit(self, url):
self.driver.get(url)
def input(self, selector, value, wait=True):
if wait: self.wait.until(visible((By.CSS_SELECTOR, selector)))
self.driver.find_element_by_css_selector(selector).send_keys(value)
def click(self, selector, wait=True):
if wait: self.wait.until(clickable((By.CSS_SELECTOR, selector)))
self.driver.find_element_by_css_selector(selector).click()
# Dropdowns
def select(self, selector, value):
select = Select(self.driver.find_element_by_css_selector(selector))
select.select_by_value(value)
def html(self):
html = self.driver.find_element_by_tag_name('html').get_attribute('innerHTML')
return lxml.html.fromstring(html)
def wait_for(self, selector):
self.wait.until(visible((By.CSS_SELECTOR, selector)))
@property
def url(self):
return self.driver.current_url