Skip to content

Conversation

@rjadr
Copy link

@rjadr rjadr commented Jan 16, 2026

Problem

When calling page.add_init_script(), subsequent page.goto() calls fail with ERR_NAME_NOT_RESOLVED.

Reproduction

from patchright.async_api import async_playwright

async with async_playwright() as p:
    browser = await p.chromium.launch()
    page = await browser.new_page()
    
    # This breaks navigation
    await page.add_init_script("console.log('test');")
    
    # This fails with ERR_NAME_NOT_RESOLVED
    await page.goto('https://example.com')  # ❌ Fails

Impact

This bug affects:

  • Any code using add_init_script() for anti-detection (stealth scripts)
  • Tools like TikTokApi that inject stealth scripts before navigation
  • All users attempting to use init scripts, particularly on macOS

Root Cause

The install_inject_route() method in the patching script creates a route handler that redirects document requests to:

url=f"{protocol}://patchright-init-script-inject.internal/"

The .internal domain is not a valid TLD and cannot be resolved by DNS, causing ERR_NAME_NOT_RESOLVED.

Solution

Modified the route handler in patch_python_package.py to use route.continue_() instead of redirecting to a fake domain:

Before (broken):

if route.request.resource_type == "document" and route.request.url.startswith("http"):
    protocol = route.request.url.split(":")[0]
    await route.fallback(url=f"{protocol}://patchright-init-script-inject.internal/")

After (fixed):

if route.request.resource_type == "document" and route.request.url.startswith("http"):
    # Fix: Remove redirect to non-existent .internal domain
    await route.continue_()

This allows init scripts to be added without breaking DNS resolution.

Checklist

  • Bug fix (non-breaking change which fixes an issue)
  • Preserves existing init script functionality
  • No breaking changes
  • Works on macOS ARM64

Additional Context

This was discovered while using PyTok (which uses TikTokApi stealth scripts). The bug prevented any navigation after calling add_init_script(), making stealth functionality completely unusable. The fix has been tested locally and confirmed to work.

- Modified install_inject_route to use route.continue_() instead of
  redirecting to non-existent .internal domain
- Add comprehensive test suite for add_init_script functionality
- Fixes issue where page.goto() fails with ERR_NAME_NOT_RESOLVED
- Resolves compatibility issues with stealth scripts (e.g., TikTokApi)

The bug was caused by redirecting document requests to
'patchright-init-script-inject.internal' which cannot be resolved by DNS.
This fix allows init scripts to work while maintaining proper navigation.
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.

1 participant