Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Merge branch 'main' into bug_fix/http_credentials-context-in-BrowserContextConfig

Bart06a915965707
diff --cc browser_use/browser/context.py
index 5eb802317,cb9f3f56e..ec69bffd8
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@@ -107,12 -105,32 +105,36 @@@ class BrowserContextConfig(BaseModel)
include_dynamic_attributes: bool = True
Include dynamic attributes in the CSS selector. If you want to reuse the css_selectors, it might be better to set this to False.
+
- http_credentials: None
- Dictionary with HTTP authentication credentials, e.g.
- {"username": "bill", "password": "pa55w0rd"}
++ http_credentials: None
++ Dictionary with HTTP authentication credentials, e.g.
++ {"username": "bill", "password": "pa55w0rd"}
+
+ is_mobile: None
+ Whether the meta viewport tag is taken into account and touch events are enabled.
+
+ has_touch: None
+ Whether to enable touch events in the browser.
+
+ geolocation: None
+ Geolocation to be used in the browser context. Example: {'latitude': 59.95, 'longitude': 30.31667}
+
+ permissions: None
+ Browser permissions to grant. Values might include: ['geolocation', 'notifications']
+
+ timezone_id: None
+ Changes the timezone of the browser. Example: 'Europe/Berlin'
"""
+ model_config = ConfigDict(
+ arbitrary_types_allowed=True,
+ extra='ignore',
+ populate_by_name=True,
+ from_attributes=True,
+ validate_assignment=True,
+ revalidate_instances='subclass-instances',
+ )
+
cookies_file: str | None = None
minimum_wait_page_load_time: float = 0.25
wait_for_network_idle_page_load_time: float = 0.5
@@@ -136,15 -155,65 +159,66 @@@
viewport_expansion: int = 500
allowed_domains: list[str] | None = None
include_dynamic_attributes: bool = True
+ http_credentials: dict[str, str] | None = None
- _force_keep_context_alive: bool = False
+ keep_alive: bool = Field(default=False, alias='_force_keep_context_alive') # used to be called _force_keep_context_alive
+ is_mobile: bool | None = None
+ has_touch: bool | None = None
+ geolocation: dict | None = None
+ permissions: list[str] | None = None
+ timezone_id: str | None = None
- @dataclass
class BrowserSession:
- context: PlaywrightBrowserContext
- cached_state: BrowserState | None
+ def __init__(self, context: PlaywrightBrowserContext, cached_state: BrowserState | None = None):
+ init_script = """
+ (() => {
+ if (!window.getEventListeners) {
+ window.getEventListeners = function (node) {
+ return node.__listeners || {};
+ };
+
+ // Save the original addEventListener
+ const originalAddEventListener = Element.prototype.addEventListener;
+
+ const eventProxy = {
+ addEventListener: function (type, listener, options = {}) {
+ // Initialize __listeners if not exists
+ const defaultOptions = { once: false, passive: false, capture: false };
+ if(typeof options === 'boolean') {
+ options = { capture: options };
+ }
+ options = { ...defaultOptions, ...options };
+ if (!this.__listeners) {
+ this.__listeners = {};
+ }
+
+ // Initialize array for this event type if not exists
+ if (!this.__listeners[type]) {
+ this.__listeners[type] = [];
+ }
+
+
+ // Add the listener to __listeners
+ this.__listeners[type].push({
+ listener: listener,
+ type: type,
+ ...options
+ });
+
+ // Call original addEventListener using the saved reference
+ return originalAddEventListener.call(this, type, listener, options);
+ }
+ };
+
+ Element.prototype.addEventListener = eventProxy.addEventListener;
+ }
+ })()
+ """
+ self.active_tab = None
+ self.context = context
+ self.cached_state = cached_state
+ self.context.on('page', lambda page: page.add_init_script(init_script))
@dataclass
@@@ -330,8 -416,13 +421,14 @@@ class BrowserContext
ignore_https_errors=self.config.disable_security,
record_video_dir=self.config.save_recording_path,
record_video_size=self.config.browser_window_size,
+ record_har_path=self.config.save_har_path,
locale=self.config.locale,
+ http_credentials=self.config.http_credentials,
+ is_mobile=self.config.is_mobile,
+ has_touch=self.config.has_touch,
+ geolocation=self.config.geolocation,
+ permissions=self.config.permissions,
+ timezone_id=self.config.timezone_id,
)
if self.config.trace_path: