|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | try:
|
3 |
| - from selenium import webdriver |
4 |
| - #to add capabilities for chrome and firefox, import their Options with different aliases |
5 |
| - from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 3 | + from seleniumwire import webdriver |
| 4 | + # to add capabilities for chrome and firefox, import their Options with different aliases |
| 5 | + from selenium.webdriver.chrome.options import Options as ChromeOptions |
6 | 6 | from selenium.webdriver.firefox.options import Options as FirefoxOptions
|
7 |
| - #import webdriver for downloading respective driver for the browser |
| 7 | + # import webdriver for downloading respective driver for the browser |
8 | 8 | from webdriver_manager.chrome import ChromeDriverManager
|
9 | 9 | from webdriver_manager.firefox import GeckoDriverManager
|
10 | 10 | except Exception as ex:
|
|
13 | 13 |
|
14 | 14 | class Initializer:
|
15 | 15 |
|
16 |
| - def __init__(self,browser_name): |
| 16 | + def __init__(self, browser_name, proxy=None): |
17 | 17 | self.browser_name = browser_name
|
| 18 | + self.proxy = proxy |
18 | 19 |
|
19 |
| - def set_properties(self,browser_option): |
| 20 | + def set_properties(self, browser_option): |
20 | 21 | """adds capabilities to the driver"""
|
21 |
| - browser_option.add_argument('--headless') #runs browser in headless mode |
22 |
| - browser_option.add_argument('--no-sandbox') |
23 |
| - |
| 22 | + browser_option.add_argument( |
| 23 | + '--headless') # runs browser in headless mode |
| 24 | + browser_option.add_argument('--no-sandbox') |
24 | 25 | browser_option.add_argument("--disable-dev-shm-usage")
|
25 | 26 | browser_option.add_argument('--ignore-certificate-errors')
|
26 | 27 | browser_option.add_argument('--disable-gpu')
|
27 | 28 | browser_option.add_argument('--log-level=3')
|
28 | 29 | browser_option.add_argument('--disable-notifications')
|
29 | 30 | browser_option.add_argument('--disable-popup-blocking')
|
| 31 | + |
| 32 | + # browser_option.add_argument( |
| 33 | + # "--proxy-server=http://{}".format(self.proxy.replace(" ", ""))) |
| 34 | + |
30 | 35 | return browser_option
|
31 | 36 |
|
32 |
| - def set_driver_for_browser(self,browser_name): |
| 37 | + def set_driver_for_browser(self, browser_name): |
33 | 38 | """expects browser name and returns a driver instance"""
|
34 |
| - #if browser is suppose to be chrome |
| 39 | + # if browser is suppose to be chrome |
35 | 40 | if browser_name.lower() == "chrome":
|
36 | 41 | browser_option = ChromeOptions()
|
37 |
| - #automatically installs chromedriver and initialize it and returns the instance |
38 |
| - return webdriver.Chrome(executable_path=ChromeDriverManager().install(),options=self.set_properties(browser_option)) |
| 42 | + # automatically installs chromedriver and initialize it and returns the instance |
| 43 | + if self.proxy is not None: |
| 44 | + options = { |
| 45 | + 'https': 'https://{}'.format(self.proxy.replace(" ", "")), |
| 46 | + 'http': 'http://{}'.format(self.proxy.replace(" ", "")), |
| 47 | + 'no_proxy': 'localhost, 127.0.0.1' |
| 48 | + } |
| 49 | + print("Using: {}".format(self.proxy)) |
| 50 | + return webdriver.Chrome(executable_path=ChromeDriverManager().install(), |
| 51 | + options=self.set_properties(browser_option), seleniumwire_options=options) |
| 52 | + |
| 53 | + return webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=self.set_properties(browser_option)) |
39 | 54 | elif browser_name.lower() == "firefox":
|
40 | 55 | browser_option = FirefoxOptions()
|
41 |
| - #automatically installs geckodriver and initialize it and returns the instance |
42 |
| - return webdriver.Firefox(executable_path=GeckoDriverManager().install(),options=self.set_properties(browser_option)) |
| 56 | + if self.proxy is not None: |
| 57 | + options = { |
| 58 | + 'https': 'https://{}'.format(self.proxy.replace(" ", "")), |
| 59 | + 'http': 'http://{}'.format(self.proxy.replace(" ", "")), |
| 60 | + 'no_proxy': 'localhost, 127.0.0.1' |
| 61 | + } |
| 62 | + print("Using: {}".format(self.proxy)) |
| 63 | + return webdriver.Firefox(executable_path=GeckoDriverManager().install(), |
| 64 | + options=self.set_properties(browser_option), seleniumwire_options=options) |
| 65 | + |
| 66 | + # automatically installs geckodriver and initialize it and returns the instance |
| 67 | + return webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=self.set_properties(browser_option)) |
43 | 68 | else:
|
44 |
| - #if browser_name is not chrome neither firefox than raise an exception |
| 69 | + # if browser_name is not chrome neither firefox than raise an exception |
45 | 70 | raise Exception("Browser not supported!")
|
| 71 | + |
46 | 72 | def init(self):
|
47 | 73 | """returns driver instance"""
|
48 | 74 | driver = self.set_driver_for_browser(self.browser_name)
|
|
0 commit comments