Fix proxy authentication ERR_INVALID_AUTH_CREDENTIALS

- Fix dict-to-ProxyConfig conversion in BrowserConfig and CrawlerRunConfig
- Fix JSON serialization of ProxyConfig objects in to_dict methods
- Fix context proxy to use ProxySettings instead of plain dict
- Resolves proxy authentication issues
This commit is contained in:
Gary
2025-07-08 17:55:28 +08:00
parent 02f3127ded
commit 1d6efb622d
2 changed files with 10 additions and 12 deletions

View File

@@ -510,7 +510,7 @@ class BrowserConfig:
chrome_channel=kwargs.get("chrome_channel", "chromium"), chrome_channel=kwargs.get("chrome_channel", "chromium"),
channel=kwargs.get("channel", "chromium"), channel=kwargs.get("channel", "chromium"),
proxy=kwargs.get("proxy"), proxy=kwargs.get("proxy"),
proxy_config=kwargs.get("proxy_config", None), proxy_config=ProxyConfig.from_dict(kwargs.get("proxy_config")) if isinstance(kwargs.get("proxy_config"), dict) else kwargs.get("proxy_config", None),
viewport_width=kwargs.get("viewport_width", 1080), viewport_width=kwargs.get("viewport_width", 1080),
viewport_height=kwargs.get("viewport_height", 600), viewport_height=kwargs.get("viewport_height", 600),
accept_downloads=kwargs.get("accept_downloads", False), accept_downloads=kwargs.get("accept_downloads", False),
@@ -546,7 +546,7 @@ class BrowserConfig:
"chrome_channel": self.chrome_channel, "chrome_channel": self.chrome_channel,
"channel": self.channel, "channel": self.channel,
"proxy": self.proxy, "proxy": self.proxy,
"proxy_config": self.proxy_config, "proxy_config": self.proxy_config.to_dict() if hasattr(self.proxy_config, 'to_dict') else self.proxy_config,
"viewport_width": self.viewport_width, "viewport_width": self.viewport_width,
"viewport_height": self.viewport_height, "viewport_height": self.viewport_height,
"accept_downloads": self.accept_downloads, "accept_downloads": self.accept_downloads,
@@ -1121,7 +1121,7 @@ class CrawlerRunConfig():
prettiify=kwargs.get("prettiify", False), prettiify=kwargs.get("prettiify", False),
parser_type=kwargs.get("parser_type", "lxml"), parser_type=kwargs.get("parser_type", "lxml"),
scraping_strategy=kwargs.get("scraping_strategy"), scraping_strategy=kwargs.get("scraping_strategy"),
proxy_config=kwargs.get("proxy_config"), proxy_config=ProxyConfig.from_dict(kwargs.get("proxy_config")) if isinstance(kwargs.get("proxy_config"), dict) else kwargs.get("proxy_config"),
proxy_rotation_strategy=kwargs.get("proxy_rotation_strategy"), proxy_rotation_strategy=kwargs.get("proxy_rotation_strategy"),
# Browser Location and Identity Parameters # Browser Location and Identity Parameters
locale=kwargs.get("locale", None), locale=kwargs.get("locale", None),
@@ -1234,7 +1234,7 @@ class CrawlerRunConfig():
"prettiify": self.prettiify, "prettiify": self.prettiify,
"parser_type": self.parser_type, "parser_type": self.parser_type,
"scraping_strategy": self.scraping_strategy, "scraping_strategy": self.scraping_strategy,
"proxy_config": self.proxy_config, "proxy_config": self.proxy_config.to_dict() if hasattr(self.proxy_config, 'to_dict') else self.proxy_config,
"proxy_rotation_strategy": self.proxy_rotation_strategy, "proxy_rotation_strategy": self.proxy_rotation_strategy,
"locale": self.locale, "locale": self.locale,
"timezone_id": self.timezone_id, "timezone_id": self.timezone_id,

View File

@@ -858,14 +858,12 @@ class BrowserManager:
if crawlerRunConfig: if crawlerRunConfig:
# Check if there is value for crawlerRunConfig.proxy_config set add that to context # Check if there is value for crawlerRunConfig.proxy_config set add that to context
if crawlerRunConfig.proxy_config: if crawlerRunConfig.proxy_config:
proxy_settings = { from playwright.async_api import ProxySettings
"server": crawlerRunConfig.proxy_config.server, proxy_settings = ProxySettings(
} server=crawlerRunConfig.proxy_config.server,
if crawlerRunConfig.proxy_config.username: username=crawlerRunConfig.proxy_config.username,
proxy_settings.update({ password=crawlerRunConfig.proxy_config.password,
"username": crawlerRunConfig.proxy_config.username, )
"password": crawlerRunConfig.proxy_config.password,
})
context_settings["proxy"] = proxy_settings context_settings["proxy"] = proxy_settings
if self.config.text_mode: if self.config.text_mode: