-
Notifications
You must be signed in to change notification settings - Fork 88
/
example.py
88 lines (61 loc) · 2.23 KB
/
example.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
import os
import logging
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver import firefox
logging.getLogger().setLevel(logging.INFO)
BASE_URL = 'http://www.example.com/'
def chrome_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_experimental_option('prefs', {
'download.default_directory': os.getcwd(),
'download.prompt_for_download': False,
})
logging.info('Prepared chrome options..')
browser = webdriver.Chrome(options=chrome_options)
logging.info('Initialized chrome browser..')
browser.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)
logging.info('Page title: %s', browser.title)
browser.quit()
display.stop()
def firefox_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')
firefox_options = firefox.options.Options()
firefox_options.set_preference('browser.download.folderList', 2)
firefox_options.set_preference(
'browser.download.manager.showWhenStarting', False
)
firefox_options.set_preference('browser.download.dir', os.getcwd())
firefox_options.set_preference(
'browser.helperApps.neverAsk.saveToDisk', 'text/csv'
)
logging.info('Prepared firefox profile..')
browser = webdriver.Firefox(options=firefox_options)
logging.info('Initialized firefox browser..')
browser.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)
logging.info('Page title: %s', browser.title)
browser.quit()
display.stop()
def phantomjs_example():
display = Display(visible=0, size=(800, 600))
display.start()
logging.info('Initialized virtual display..')
browser = webdriver.PhantomJS()
logging.info('Initialized phantomjs browser..')
browser.get(BASE_URL)
logging.info('Accessed %s ..', BASE_URL)
logging.info('Page title: %s', browser.title)
browser.quit()
display.stop()
if __name__ == '__main__':
chrome_example()
firefox_example()
phantomjs_example()