Skip to content

fix: set the lang browser flag from Config instead of add_argument - #272

Open
bercedev wants to merge 1 commit into
cdpdriver:mainfrom
bercedev:fix/lang-config-argument
Open

fix: set the lang browser flag from Config instead of add_argument#272
bercedev wants to merge 1 commit into
cdpdriver:mainfrom
bercedev:fix/lang-config-argument

Conversation

@bercedev

Copy link
Copy Markdown

Closes #262.

The problem

Browser.start appends the flag through Config.add_argument:

# zendriver/core/browser.py
if self.config.lang is not None:
    self.config.add_argument(f"--lang={self.config.lang}")

but add_argument rejects any argument containing "lang":

# zendriver/core/config.py
def add_argument(self, arg: str) -> None:
    if any(x in arg.lower() for x in ["headless", "data-dir", "data_dir", "no-sandbox", "no_sandbox", "lang"]):
        raise ValueError(
            '"%s" not allowed. please use one of the attributes of the Config object to set it' % arg
        )

So setting the attribute the error message points to is exactly what triggers the error. lang is unusable through every entry point:

import asyncio
import zendriver as zd

async def main():
    browser = await zd.start(lang="en-US")
    await browser.stop()

asyncio.run(main())
  File "zendriver/core/browser.py", line 362, in start
    self.config.add_argument(f"--lang={self.config.lang}")
  File "zendriver/core/config.py", line 243, in add_argument
    raise ValueError(...)
ValueError: "--lang=en-US" not allowed. please use one of the attributes of the Config object to set it

Same for Browser.create(lang=...) and Config(lang=...). The only way to get the flag through today is to bypass the attribute entirely — Config(browser_args=["--lang=en-US"]) works, because arguments passed to the constructor are not validated.

The fix

Emit the flag from Config.__call__, next to the other attribute-backed flags:

if self.user_agent:
    args.append(f"--user-agent={self.user_agent}")
if self.lang:
    args.append(f"--lang={self.lang}")
if not self.sandbox:
    args.append("--no-sandbox")

and drop the add_argument call from Browser.start. This matches how headless, user_agent and sandbox are already handled, keeps the add_argument guard intact, and needs no new API.

Tests

  • tests/core/test_config.py (new): the flag is emitted when lang is set, omitted when it is not, composes with browser_args, and add_argument("--lang=…") still raises.
  • tests/core/test_browser.py: a browser actually starts with lang set — this is the regression that produced the ValueError.
  • tests/conftest.py: CreateBrowser accepts lang so the test above can pass it.

Verified with scripts/lint.sh (ruff + mypy clean) and the tests above on Chromium 151.

Note

Worth flagging separately: --lang does not change navigator.language, which keeps reporting the browser's installed UI language. That is Chromium behaviour rather than something this PR changes, so I left it alone — happy to open a separate issue if you would like it documented.

Browser.start appended `--lang=<lang>` through Config.add_argument, but
add_argument rejects any argument containing "lang" and tells the caller to
use a Config attribute instead. Setting that very attribute therefore always
raised ValueError, which made the `lang` option unusable through every entry
point (zd.start(lang=...), Browser.create(lang=...), Config(lang=...)).

Emit the flag from Config.__call__ alongside the other attribute-backed flags
(headless, user_agent, sandbox), so the add_argument guard stays intact and
lang behaves like every other Config option.
@bercedev
bercedev requested a review from a team as a code owner August 21, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG?] language setup is contradictory?

2 participants