|
10 | 10 | import typing
|
11 | 11 | from contextlib import suppress
|
12 | 12 | from seleniumbase import config as sb_config
|
| 13 | +from seleniumbase import extensions |
13 | 14 | from seleniumbase.config import settings
|
14 | 15 | from seleniumbase.core import detect_b_ver
|
| 16 | +from seleniumbase.core import download_helper |
15 | 17 | from seleniumbase.core import proxy_helper
|
16 | 18 | from seleniumbase.fixtures import constants
|
17 | 19 | from seleniumbase.fixtures import shared_utils
|
|
25 | 27 |
|
26 | 28 | logger = logging.getLogger(__name__)
|
27 | 29 | IS_LINUX = shared_utils.is_linux()
|
| 30 | +DOWNLOADS_FOLDER = download_helper.get_downloads_folder() |
28 | 31 | PROXY_DIR_LOCK = proxy_helper.PROXY_DIR_LOCK
|
| 32 | +EXTENSIONS_DIR = os.path.dirname(os.path.realpath(extensions.__file__)) |
| 33 | +AD_BLOCK_ZIP_PATH = os.path.join(EXTENSIONS_DIR, "ad_block.zip") |
29 | 34 | T = typing.TypeVar("T")
|
30 | 35 |
|
31 | 36 |
|
@@ -168,6 +173,19 @@ def __add_chrome_ext_dir(extension_dir, dir_path):
|
168 | 173 | return extension_dir
|
169 | 174 |
|
170 | 175 |
|
| 176 | +def __unzip_to_new_folder(zip_file, folder): |
| 177 | + proxy_dir_lock = fasteners.InterProcessLock(PROXY_DIR_LOCK) |
| 178 | + with proxy_dir_lock: |
| 179 | + with suppress(Exception): |
| 180 | + shared_utils.make_writable(PROXY_DIR_LOCK) |
| 181 | + if not os.path.exists(folder): |
| 182 | + import zipfile |
| 183 | + zip_ref = zipfile.ZipFile(zip_file, "r") |
| 184 | + os.makedirs(folder) |
| 185 | + zip_ref.extractall(folder) |
| 186 | + zip_ref.close() |
| 187 | + |
| 188 | + |
171 | 189 | def __add_chrome_proxy_extension(
|
172 | 190 | extension_dir,
|
173 | 191 | proxy_string,
|
@@ -231,14 +249,18 @@ async def start(
|
231 | 249 | browser_executable_path: Optional[PathLike] = None,
|
232 | 250 | browser_args: Optional[List[str]] = None,
|
233 | 251 | xvfb_metrics: Optional[List[str]] = None, # "Width,Height" for Linux
|
| 252 | + ad_block: Optional[bool] = False, |
234 | 253 | sandbox: Optional[bool] = True,
|
235 | 254 | lang: Optional[str] = None, # Set the Language Locale Code
|
236 | 255 | host: Optional[str] = None, # Chrome remote-debugging-host
|
237 | 256 | port: Optional[int] = None, # Chrome remote-debugging-port
|
238 | 257 | xvfb: Optional[int] = None, # Use a special virtual display on Linux
|
239 | 258 | headed: Optional[bool] = None, # Override default Xvfb mode on Linux
|
240 | 259 | expert: Optional[bool] = None, # Open up closed Shadow-root elements
|
| 260 | + agent: Optional[str] = None, # Set the user-agent string |
241 | 261 | proxy: Optional[str] = None, # "host:port" or "user:pass@host:port"
|
| 262 | + tzone: Optional[str] = None, # Eg "America/New_York", "Asia/Kolkata" |
| 263 | + geoloc: Optional[list | tuple] = None, # Eg (48.87645, 2.26340) |
242 | 264 | extension_dir: Optional[str] = None, # Chrome extension directory
|
243 | 265 | **kwargs: Optional[dict],
|
244 | 266 | ) -> Browser:
|
@@ -296,6 +318,13 @@ async def start(
|
296 | 318 | proxy_user,
|
297 | 319 | proxy_pass,
|
298 | 320 | )
|
| 321 | + if ad_block: |
| 322 | + incognito = False |
| 323 | + guest = False |
| 324 | + ad_block_zip = AD_BLOCK_ZIP_PATH |
| 325 | + ad_block_dir = os.path.join(DOWNLOADS_FOLDER, "ad_block") |
| 326 | + __unzip_to_new_folder(ad_block_zip, ad_block_dir) |
| 327 | + extension_dir = __add_chrome_ext_dir(extension_dir, ad_block_dir) |
299 | 328 | if not config:
|
300 | 329 | config = Config(
|
301 | 330 | user_data_dir,
|
@@ -329,6 +358,26 @@ async def start(
|
329 | 358 | sb_config._cdp_locale = kwargs["locale_code"]
|
330 | 359 | else:
|
331 | 360 | sb_config._cdp_locale = None
|
| 361 | + if tzone: |
| 362 | + sb_config._cdp_timezone = tzone |
| 363 | + elif "timezone" in kwargs: |
| 364 | + sb_config._cdp_timezone = kwargs["timezone"] |
| 365 | + else: |
| 366 | + sb_config._cdp_timezone = None |
| 367 | + if geoloc: |
| 368 | + sb_config._cdp_geolocation = geoloc |
| 369 | + elif "geolocation" in kwargs: |
| 370 | + sb_config._cdp_geolocation = kwargs["geolocation"] |
| 371 | + else: |
| 372 | + sb_config._cdp_geolocation = None |
| 373 | + if agent: |
| 374 | + sb_config._cdp_user_agent = agent |
| 375 | + elif "user_agent" in kwargs: |
| 376 | + sb_config._cdp_user_agent = kwargs["user_agent"] |
| 377 | + else: |
| 378 | + sb_config._cdp_user_agent = None |
| 379 | + if "platform" in kwargs: |
| 380 | + sb_config._cdp_platform = kwargs["platform"] |
332 | 381 | return driver
|
333 | 382 |
|
334 | 383 |
|
|
0 commit comments