Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to turn off log errors generated by interception #1185

Closed
jw-star opened this issue Mar 3, 2022 · 9 comments
Closed

[Question] How to turn off log errors generated by interception #1185

jw-star opened this issue Mar 3, 2022 · 9 comments
Labels

Comments

@jw-star
Copy link

jw-star commented Mar 3, 2022

async def no_static(route, req):
    if req.resource_type in {"image", "media", "font"}:
        try:
            await route.fulfill(status=404, content_type="text/plain", body="not found!")
        except:
            pass
    else:
        await  route.continue_()
future: <Task finished name='Task-320' coro=<Channel.send() done, defined at C:\Users\jiawei\AppData\Local\Programs\Python\Python310\lib\site-packages\playwright\_impl\_connection.py:38> exception=Error('Route is already handled!')>
Traceback (most recent call last):
  File "C:\Users\jiawei\AppData\Local\Programs\Python\Python310\lib\site-packages\playwright\_impl\_connection.py", line 39, in send
    return await self.inner_send(method, params, False)
  File "C:\Users\jiawei\AppData\Local\Programs\Python\Python310\lib\site-packages\playwright\_impl\_connection.py", line 63, in inner_send
    result = next(iter(done)).result()
playwright._impl._api_types.Error: Route is already handled!
@aslushnikov aslushnikov transferred this issue from microsoft/playwright Mar 4, 2022
@mxschmitt
Copy link
Member

Could you provide us a full example of your code? I tried the following which was working fine for me:

import asyncio
from playwright.async_api import async_playwright

async def no_static(route, req):
    if req.resource_type in {"image", "media", "font"}:
        try:
            await route.fulfill(status=404, content_type="text/plain", body="not found!")
        except:
            pass
    else:
        await  route.continue_()

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.route("**/*", no_static)
        await page.goto('http://microsoft.com/')
        await page.screenshot(path=f'example.png')
        await browser.close()

asyncio.run(main())

Make sure that you only call a single time fulfill/continue/abort. Otherwise "Route is already handled! gets thrown.

@BruceLee569
Copy link

BruceLee569 commented May 7, 2022

Could you provide us a full example of your code? I tried the following which was working fine for me:

import asyncio
from playwright.async_api import async_playwright

async def no_static(route, req):
    if req.resource_type in {"image", "media", "font"}:
        try:
            await route.fulfill(status=404, content_type="text/plain", body="not found!")
        except:
            pass
    else:
        await  route.continue_()

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.route("**/*", no_static)
        await page.goto('http://microsoft.com/')
        await page.screenshot(path=f'example.png')
        await browser.close()

asyncio.run(main())

Make sure that you only call a single time fulfill/continue/abort. Otherwise "Route is already handled! gets thrown.

I also encountered this problem, calling function context.route() will throw this exception !

import asyncio
from playwright.async_api import async_playwright


async def no_static(route, req):
    if req.resource_type in {"image", "media", "font"}:
        try:
            await route.fulfill(status=404, content_type="text/plain", body="not found!")
        except:
            pass
    else:
        await  route.continue_()


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        await context.route('**/*', no_static)
        page = await context.new_page()
        await page.goto('http://microsoft.com/')
        await browser.close()


asyncio.run(main())

My environment: Windows10, Python 3.8.8, Playwright 1.21.0

@mxschmitt mxschmitt reopened this May 7, 2022
@saisua
Copy link

saisua commented May 15, 2022

Well, I've had this problem too, because I worry applying page.on(event) every single time I open a website might slow down things, even with Cython, so my solution was to check the source code.
The quick fix I did was to change only one line:

In playwright / _impl / _connection
 In line 63 : Channel.inner_send

result = next(iter(done)).result()
to
future = next(iter(done))
if(future.exception() is not None):
____return None
result = future.result()

The exceptions no longer print anything, so in my opinion it was worth it.

@mxschmitt
Copy link
Member

In the route handler its not supported to pass coroutines, the functions need to be sync. When modifying your repro @jw-star and removing the async and await, then it works as expected.

@BruceLee569
Copy link

In the route handler its not supported to pass coroutines, the functions need to be sync. When modifying your repro @jw-star and removing the async and await, then it works as expected.

from playwright.sync_api import sync_playwright


def no_static(route, req):
    if req.resource_type in {"image", "media", "font"}:
        try:
            route.fulfill(status=404, content_type="text/plain", body="not found!")
        except:
            pass
    else:
        route.continue_()


def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        context.route('**/*', no_static)
        page = context.new_page()
        page.goto('http://microsoft.com/')
        browser.close()


main()

I've tried synchronous code above before my last comment, and the error reporting is the same as asynchronous.

@chujian521
Copy link

I have this problem too, this error occurs when using context.route()

@chujian521
Copy link

Modify this file : ~Lib\site-packages\playwright\driver\package\lib\server\network.js
Line 250, class Route
Determine if it has been handled. If not, start Handling.

if(!this._handled){
      this._startHandling();
}

I don't know if this is the right thing to do, but it does not report any error!

@BruceLee569
Copy link

@mxschmitt Hello, how about re-open this issue?

@mxschmitt mxschmitt reopened this Jun 11, 2022
@rwoll
Copy link
Member

rwoll commented Jul 12, 2022

v1.23 introduced async route handlers. There is one known, pending issue regarding errant logs, so please follow: #1402

@rwoll rwoll closed this as completed Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants