-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdge.py
executable file
Β·152 lines (127 loc) Β· 4.52 KB
/
Edge.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def CreateBrowse(Cfg = None):
import selenium.webdriver
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Private' : True,
'DriverPath' : r'H:\GITHUB\TOOLBOX\EdgeDriver\msedgedriver.exe',
'UserDataPath' : r'H:\GITHUB\TOOLBOX\EdgeDriver\UserData',
'AntiRobotPath': r'H:\GITHUB\TOOLBOX\EdgeStealth.min.js'
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : '',
'Driver' : None,
'Private' : True
}
try:
option = selenium.webdriver.EdgeOptions()
if Config['Private']:
option.add_argument('--inprivate')
Response['Private'] = True
else:
option.add_argument('--user-data-dir=' + Config['UserDataPath'])
Response['Private'] = False
option.add_argument('--window-size=800,600')
option.add_argument('--no-sandbox')
option.add_argument('--disable-gpu')
option.add_argument('--disable-dev-shm-usage')
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-logging'])
option.add_argument("--disable-blink-features")
option.add_argument("--disable-blink-features=AutomationControlled")
service = selenium.webdriver.edge.service.Service(executable_path = Config['DriverPath'])
Response['Driver'] = selenium.webdriver.Edge(service = service, options = option)
# with open(r'H:\GITHUB\TOOLBOX\EdgeStealth.min.js') as JS:
# C = JS.read()
# edgeDriver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
# 'source': C
# })
except Exception as errorMsg:
Response['ErrorCode'] = 50000
Response['ErrorMsg'] = f'Fail to create browse, {str(errorMsg).lower().rstrip(".")}'
return Response
def CloseBrowse(Cfg = None):
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Driver': None
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode': 0,
'ErrorMsg' : ''
}
try:
Config['Driver'].quit()
except Exception as errorMsg:
Response['ErrorCode'] = 50000
Response['ErrorMsg'] = f'Fail to close browse, {str(errorMsg).lower().rstrip(".")}'
return Response
def OpenUrl(Cfg = None):
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Driver' : None,
'Url' : '',
'Ec' : False,
'EcTitle': '',
'Timeout': 10
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode' : 0,
'ErrorMsg' : '',
'Driver' : None,
'PageSource': '',
'Cookies' : []
}
try:
Config['Driver'].get(Config['Url'])
if Config['Ec']:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(Config['Driver'], Config['Timeout']).until(EC.title_contains(Config['EcTitle']))
else:
import time
time.sleep(Config['Timeout'])
Response['Driver'] = Config['Driver']
Response['PageSource'] = Config['Driver'].page_source
Response['Cookies'] = Config['Driver'].get_cookies()
except Exception as errorMsg:
Response['ErrorCode'] = 50000
Response['ErrorMsg'] = f'Fail to open url, {str(errorMsg).lower().rstrip(".")}'
return Response
def OpenBlank(Cfg = None):
if __name__ == '__main__':
from Merge import MergeCfg
else:
from .Merge import MergeCfg
Config = {
'Driver' : None
}
Config = MergeCfg(Config, Cfg)
Response = {
'ErrorCode' : 0,
'ErrorMsg' : '',
'Driver' : None,
'PageSource': '',
'Cookies' : []
}
try:
Config['Driver'].get('edge://newtab')
Response['Driver'] = Config['Driver']
Response['PageSource'] = Config['Driver'].page_source
Response['Cookies'] = Config['Driver'].get_cookies()
except Exception as errorMsg:
Response['ErrorCode'] = 50000
Response['ErrorMsg'] = f'Fail to open newtab, {str(errorMsg).lower().rstrip(".")}'
return Response