Skip to content

Commit 5c78b84

Browse files
authored
Merge branch 'main' into feat/tab-request-api-issue-171
2 parents 18248ee + ed77ff8 commit 5c78b84

File tree

6 files changed

+339
-286
lines changed

6 files changed

+339
-286
lines changed

pydoll/browser/chromium/base.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44
import shutil
5+
import warnings
56
from abc import ABC, abstractmethod
67
from contextlib import suppress
78
from functools import partial
@@ -106,20 +107,24 @@ async def start(self, headless: bool = False) -> Tab:
106107
Start browser process and establish CDP connection.
107108
108109
Args:
109-
headless: Run without UI.
110+
headless: Deprecated. Use `options.headless = True` instead.
110111
111112
Returns:
112113
Initial tab for interaction.
113114
114115
Raises:
115116
FailedToStartBrowser: If the browser fails to start or connect.
116117
"""
117-
binary_location = self.options.binary_location or self._get_default_binary_location()
118-
119118
if headless:
120-
headless_arg = '--headless'
121-
if headless_arg not in self.options.arguments:
122-
self.options.add_argument(headless_arg)
119+
warnings.warn(
120+
"The 'headless' parameter is deprecated and will be removed in a future version. "
121+
"Use `options.headless = True` instead.",
122+
DeprecationWarning,
123+
stacklevel=2
124+
)
125+
self.options.headless = headless
126+
127+
binary_location = self.options.binary_location or self._get_default_binary_location()
123128

124129
self._setup_user_dir()
125130
proxy_config = self._proxy_manager.get_proxy_credentials()

pydoll/browser/interfaces.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ def add_argument(self, argument: str):
2626
def browser_preferences(self) -> dict:
2727
pass
2828

29+
@property
30+
@abstractmethod
31+
def headless(self) -> bool:
32+
pass
33+
34+
@headless.setter
35+
@abstractmethod
36+
def headless(self, headless: bool):
37+
pass
38+
2939

3040
class BrowserOptionsManager(ABC):
3141
@abstractmethod

pydoll/browser/options.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from contextlib import suppress
22

33
from pydoll.browser.interfaces import Options
4-
from pydoll.exceptions import ArgumentAlreadyExistsInOptions, WrongPrefsDict
4+
from pydoll.exceptions import (
5+
ArgumentAlreadyExistsInOptions,
6+
ArgumentNotFoundInOptions,
7+
WrongPrefsDict,
8+
)
59

610

711
class ChromiumOptions(Options):
@@ -23,6 +27,7 @@ def __init__(self):
2327
self._binary_location = ''
2428
self._start_timeout = 10
2529
self._browser_preferences = {}
30+
self._headless = False
2631

2732
@property
2833
def arguments(self) -> list[str]:
@@ -99,6 +104,20 @@ def add_argument(self, argument: str):
99104
else:
100105
raise ArgumentAlreadyExistsInOptions(f'Argument already exists: {argument}')
101106

107+
def remove_argument(self, argument: str):
108+
"""
109+
Removes a command-line argument from the options.
110+
111+
Args:
112+
argument (str): The command-line argument to be removed.
113+
114+
Raises:
115+
ArgumentNotFoundInOptions: If the argument is not in the list of arguments.
116+
"""
117+
if argument not in self._arguments:
118+
raise ArgumentNotFoundInOptions(f'Argument not found: {argument}')
119+
self._arguments.remove(argument)
120+
102121
@property
103122
def browser_preferences(self) -> dict:
104123
return self._browser_preferences
@@ -284,3 +303,19 @@ def open_pdf_externally(self, enabled: bool):
284303
block -- If True, location access is blocked (value = 2); otherwise allowed (value = 1).
285304
"""
286305
self._set_pref_path(['plugins', 'always_open_pdf_externally'], enabled)
306+
307+
@property
308+
def headless(self) -> bool:
309+
return self._headless
310+
311+
@headless.setter
312+
def headless(self, headless: bool):
313+
self._headless = headless
314+
has_argument = '--headless' in self.arguments
315+
methods_map = {
316+
True: self.add_argument,
317+
False: self.remove_argument
318+
}
319+
if headless == has_argument:
320+
return
321+
methods_map[headless]('--headless')

pydoll/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ class ArgumentAlreadyExistsInOptions(ConfigurationException):
199199
message = 'The argument already exists in the options'
200200

201201

202+
class ArgumentNotFoundInOptions(ConfigurationException):
203+
"""Raised when attempting to remove an argument that does not exist in browser options."""
204+
205+
message = 'The argument does not exist in the options'
206+
207+
202208
class InvalidFileExtension(ConfigurationException):
203209
"""Raised when an unsupported file extension is provided."""
204210

0 commit comments

Comments
 (0)