-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_manager.py
66 lines (60 loc) Β· 3.49 KB
/
driver_manager.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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.edge.options import Options as EdgeOptions
from config.config import DRIVER_PATH_CHROME, DRIVER_PATH_FIREFOX, DRIVER_PATH_EDGE
class WebDriverConf:
def __init__(self, driver_choice, device):
self.driver_choice = driver_choice
self.device = device
def get_driver(self):
if self.driver_choice == 1: # Chrome
service = ChromeService(DRIVER_PATH_CHROME)
options = ChromeOptions()
if self.device == 'm':
options.add_argument("--disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/14A345 Safari/604.1")
options.add_argument("viewport-size=360,812")
return webdriver.Chrome(service=service, options=options)
elif self.device == 'd':
return webdriver.Chrome(service=service, options=None)
elif self.driver_choice == 2: # Firefox
service = FirefoxService(DRIVER_PATH_FIREFOX)
service.log_output = None
options = FirefoxOptions()
options.add_argument("--window-size=360,812")
if self.device == 'm':
options.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/14A345 Safari/604.1")
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "proxy.com")
options.set_preference("network.proxy.http_port", 8080)
return webdriver.Firefox(service=service, options=options)
elif self.device == 'd':
return webdriver.Firefox(service=service, options=None)
elif self.driver_choice == 3: # Edge
service = EdgeService(DRIVER_PATH_EDGE)
options = EdgeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument("--ignore-ssl-errors")
if self.device == 'm':
options.add_argument("--disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=360,812")
options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'})
return webdriver.Edge(service=service, options=options)
elif self.device == 'd':
return webdriver.Edge(service=service, options=None)
else:
raise ValueError("Invalid browser choice")
if __name__ == "__main__":
print("This is driver manager file. Don't run this file directly.")