Skip to content

Commit 84f3d16

Browse files
author
kaliiiiiiiiii
committed
- added profile["cdp"]["patch_version"] | fix for #26
- removed deprecated profile["options"]["browser"]["mobile_view"] - removed deprecated profile["options"]["device"]
1 parent dd107ea commit 84f3d16

File tree

3 files changed

+88
-76
lines changed

3 files changed

+88
-76
lines changed

README.md

+26-23
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,44 @@ in the End. Else-wise your temporary folder will get flooded!
5454
## Profiles
5555

5656
Example Profile:
57-
```json
57+
```python
58+
profile = \
5859
{
5960
"options": {
6061
"browser": {
61-
"sandbox": true,
62+
"sandbox": True,
6263
"window_size": {"x":1024,"y":648},
63-
"headless": false,
64-
"load_images": true,
65-
"incognito": true,
66-
"app": false,
67-
"gpu": false,
68-
"proxy": null,
69-
"proxy_method": null
64+
"headless": False,
65+
"load_images": True,
66+
"incognito": True,
67+
"app": False,
68+
"touch": True, #
69+
"gpu": False,
70+
"proxy": "example-proxy.com:9000", # note: auth not supported
71+
"proxy_method": "http://" # optional, default: "socks5://"
7072
},
7173
"extensions": {
72-
"extension_paths": [],
74+
"extension_paths": ["path/to/extension_1"], # directory, .crx or .zip
7375
"auth_proxy": {"host":"host","port":9000,"username":"user", "password":"password", "temp_dir": "C:/Downloads/proxy_extension"}
7476
},
7577
"option_args": ["--my-arg1", "..."],
7678
"capabilities": [],
77-
"adb": false,
79+
"adb": False,
7880
"adb_package": "com.android.chrome",
79-
"use_running_app": true
81+
"use_running_app": True
8082
},
8183
"cdp": {
8284
"browser": {
83-
"pointer_as_touch": false,
84-
"darkmode": false,
85-
"mobile": true
85+
"pointer_as_touch": False,
86+
"darkmode": False,
87+
"mobile": True
8688
},
87-
"touch": true,
89+
"touch": True,
8890
"maxtouchpoints": 5,
8991
"cdp_args": [],
90-
"emulation": {"mobile":true,"width": 384, "height": 700, "deviceScaleFactor": 10,
92+
"emulation": {"mobile":True,"width": 384, "height": 700, "deviceScaleFactor": 10,
9193
"screenOrientation": {"type": "portrait-primary", "angle": 0}},
94+
"patch_version": True, # to patch automatically, or use "111.0.5563.111"
9295
"useragent": {
9396
"platform": "Linux aarch64",
9497
"acceptLanguage":"en-US",
@@ -104,10 +107,11 @@ Example Profile:
104107
"platformVersion": "11.0.0",
105108
"architecture": "",
106109
"model": "HD1913",
107-
"mobile": true,
110+
"mobile": True,
108111
"bitness": "",
109-
"wow64": false}
110-
}}
112+
"wow64": False}
113+
}
114+
}
111115
}
112116
```
113117

@@ -192,12 +196,11 @@ Please feel free to open an issue or fork!
192196
- [ ] [Chrome-Browser](https://www.google.de/chrome/) (silent install)
193197
- [x] js-undetectability
194198
- [ ] [`navigator.connection`]
195-
- [ ] fonts don't match platform
196-
- change fonts using extension [font-settings](https://developer.chrome.com/docs/extensions/reference/fontSettings)
199+
- [ ] fonts don't match platform
197200
- [ ] does not match worker scope (Emulation)
198201
- `Navigator.userAgent`
199202
- `Navigator.platform`
200-
- [ ] with wrong version (is:111, emulate:107)
203+
- [x] with wrong version (is:111, emulate:107) fixed with "patch_version"
201204
- v107 failed CSS features by 2 versions
202205
- v107 failed v109 Window features
203206
- v107 failed v109 CSS features

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
selenium_profiles==2.2.5.3
1+
selenium_profiles>=2.2.5.4
22
requests==2.28.2
3-
selenium==4.8.2 # 4.1.0 for touch_actions
3+
selenium==4.8.3 # 4.1.0 for touch_actions
44
setuptools==67.6.0
55
undetected-chromedriver==3.4.6
66
trio

src/selenium_profiles/scripts/profiles.py

+60-51
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def __init__(self):
1111
class options: # webdriver.Chrome or uc.Chrome options
1212
def __init__(self):
1313
self.options = None
14-
self.device = self.device()
1514
self.browser = self.browser()
1615
self.extensions = self.extensions()
1716

@@ -32,7 +31,6 @@ def set(self, options, options_profile: dict or None):
3231

3332
extensions_used = profile["extensions"]
3433

35-
self.device.set(self.options, option_device_profile=profile["device"])
3634
self.browser.set(self.options, option_browser_profile=profile["browser"],
3735
extensions_used=extensions_used, adb=profile["adb"])
3836
self.extensions.set(self.options, option_extension_profile=profile["extensions"], incognito=incognito)
@@ -41,40 +39,8 @@ def set(self, options, options_profile: dict or None):
4139
# noinspection PyTypeChecker
4240
self.adb_remote(self.options, enabled=profile["adb"], package=profile["adb_package"],
4341
use_running_app=profile["use_running_app"])
44-
4542
return self.options
4643

47-
class device:
48-
def __init__(self):
49-
self.options = None
50-
51-
# noinspection PyTypeChecker
52-
def set(self,options, option_device_profile:None or dict):
53-
if option_device_profile:
54-
# noinspection PyShadowingNames
55-
profile = defaultdict(lambda: None)
56-
profile.update(option_device_profile)
57-
self.options = options
58-
59-
self.useragent(self.options,useragent=profile["useragent"])
60-
self.mobile(self.options,enabled=profile["mobile"])
61-
return self.options
62-
def useragent(self, options, useragent:str=None):
63-
# gets overwritten by cdp, if cdp used
64-
if useragent:
65-
options.add_argument('--user-agent=' + useragent)
66-
67-
return options
68-
69-
def mobile(self, options, enabled:bool or None = False):
70-
# not sure, if it changes anything?
71-
if enabled:
72-
options.add_argument('--enable-features=enable-nacl')
73-
options.add_argument('--arc-availability=officially-supported')
74-
options.add_argument('--force-device-ownership')
75-
options.add_argument('--use-mobile-user-agent')
76-
return options
77-
7844

7945
class browser:
8046
def __init__(self):
@@ -96,7 +62,6 @@ def set(self, options, option_browser_profile: dict or None, extensions_used:boo
9662
self.touch(self.options,enabled=profile["touch"])
9763
self.app(self.options, enabled=profile["app"])
9864
self.gpu(self.options, enabled=profile["gpu"], adb=adb)
99-
self.mobile_view(self.options, enabled=profile["mobile_view"])
10065
self.proxy(self.options, proxy=profile["proxy"], method=profile["proxy_method"])
10166
return self.options
10267

@@ -159,15 +124,9 @@ def gpu(self, options, enabled=False, adb:bool=None):
159124
warnings.warn('Disabling gpu not supported with android.')
160125
return options
161126

162-
def mobile_view(self, options, enabled:bool=False):
163-
if enabled:
164-
options.add_argument('--enable-touchview')
165-
warnings.warn('--enable-touchview doesn\t seem to change anything')
166-
return options
167-
168127
def proxy(self,options, proxy:str =None, method:str = 'socks5://'):
169128
if not method:
170-
method = "'socks5://'"
129+
method = "socks5://"
171130
if proxy:
172131
options.add_argument('--proxy-server='+method + proxy)
173132
print('proxy= "' + method+proxy + '"')
@@ -220,7 +179,7 @@ def add_extension(self, options, extension_paths:None or list, incognito:bool =
220179
if incognito:
221180
warnings.warn('Incognito might not be compatible with extensions')
222181
for extension_path in extension_paths:
223-
if extension_path[-4:] == ".crx" or extension_path[-3:] == ".zip":
182+
if extension_path[-4:] == ".crx" or extension_path[-4:] == ".zip":
224183
options.add_extension(extension_path)
225184
else:
226185
options.add_argument('--load-extension=' + extension_path)
@@ -255,7 +214,7 @@ def set(self, driver, cdp_profile:bool or None = None):
255214
browser = self.browser.set(driver, profile["browser"], mobile=mobile)
256215
touchpoints = self.set_touchpoints(driver, enabled=profile["touch"], maxpoints=profile["maxtouchpoints"])
257216
emulation = self.set_emulation(driver, profile["emulation"])
258-
useragent = self.set_useragent(driver, profile["useragent"])
217+
useragent = self.set_useragent(driver, profile["useragent"],patch_version=profile["patch_version"])
259218

260219
cdp_args = profile["cdp_args"]
261220
cdp_args_return = []
@@ -269,11 +228,61 @@ def set(self, driver, cdp_profile:bool or None = None):
269228
return {"browser": browser, "touchpoints": touchpoints, "emulation": emulation, "useragent": useragent,
270229
"cdp_args": cdp_args_return}
271230

272-
def set_useragent(self, driver, useragent:dict = None):
231+
def set_useragent(self, driver, useragent:dict = None, patch_version:str or bool = None):
273232
from selenium_profiles.scripts.cdp_tools import cdp_tools
274233
cdp_tools = cdp_tools(driver)
234+
useragent = self.patch_version(useragent_profile=useragent, version=patch_version, driver=driver)
275235
if useragent:
276-
return cdp_tools.set_useragent(useragent=useragent) #todo makes detected..?
236+
return cdp_tools.set_useragent(useragent=useragent)
237+
238+
def patch_version(self,useragent_profile: dict, version: str or True = True, driver=None):
239+
profile = defaultdict(lambda:None)
240+
profile.update(useragent_profile)
241+
242+
if version is True:
243+
if driver:
244+
useragent = driver.execute_script("return navigator.userAgent")
245+
import re
246+
version = re.findall("(?<=Chrome/)\d+(?:\.\d+)+|(?<=Chromium/)\d+(?:\.\d+)+", useragent)
247+
if len(version) != 1:
248+
raise LookupError("Couldn't find Chrome-version in: "+useragent)
249+
else:
250+
version = version[0]
251+
else:
252+
raise ValueError("driver needs to be specified to automatically get the Version")
253+
254+
255+
if profile["userAgent"]:
256+
import re
257+
profile["userAgent"] = re.sub("(?<=Chrome/)\d+(?:\.\d+)+|(?<=Chromium/)\d+(?:\.\d+)+",
258+
version.split(".")[0] + ".0.0.0", profile["userAgent"])
259+
260+
if profile["userAgentMetadata"]:
261+
metadata = defaultdict(lambda:None)
262+
metadata.update(profile["userAgentMetadata"])
263+
264+
if metadata["brands"]:
265+
brands = []
266+
for brand in metadata["brands"]:
267+
brand["version"] = version.split(".")[0]
268+
brands.append(brand)
269+
metadata["brands"] = brands
270+
271+
if metadata["fullVersionList"]:
272+
version_list = []
273+
for i in metadata["fullVersionList"]:
274+
i["version"] = version
275+
version_list.append(i)
276+
metadata["fullVersionList"] = version_list
277+
278+
if metadata["fullVersion"]:
279+
metadata["fullVersion"] = version
280+
281+
profile["userAgentMetadata"] = metadata
282+
283+
return profile
284+
285+
277286
def set_emulation(self, driver, emulation:dict = None):
278287
from selenium_profiles.scripts.cdp_tools import cdp_tools
279288
cdp_tools = cdp_tools(driver)
@@ -328,14 +337,14 @@ def exec_js_evaluators(profile: dict, driver, cdp_tools=None):
328337
do_return = True
329338

330339
# noinspection PyBroadException
331-
try:
332-
platform = profile["cdp"]["useragent"]["platform"]
333-
except :
334-
platform = None
340+
# try:
341+
# platform = profile["cdp"]["useragent"]["platform"]
342+
# except :
343+
# platform = None
335344

336345

337346
#if platform:
338347
# cdp_tools.define_prop_on_new_document("navigator", "platform", platform) # will be different in workers:/
339348

340349
if do_return:
341-
return cdp_tools.evaluate_on_document_identifiers
350+
return cdp_tools.evaluate_on_document_identifiers

0 commit comments

Comments
 (0)