DevTools listening on ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5 with Selenium and Python

偶尔善良 提交于 2019-11-28 14:28:58
Wahyu Bram

Based on Chanticleer in hide chromeDriver console in python

In your Python folder, find and edit this file:

Lib\site-packages\selenium\webdriver\common\services.py

Edit the Start() function by adding the creation flags this way: creationflags=CREATE_NO_WINDOW

from win32process import CREATE_NO_WINDOW

def start(self):
    """
    Starts the Service.

    :Exceptions:
     - WebDriverException : Raised either when it can't start the service
       or when it can't connect to the service
    """
    try:
        cmd = [self.path]
        cmd.extend(self.command_line_args())
        self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
    except TypeError:
        raise

It works perfectly for me using python3.7 and selenium 3.141.0

DevTools listening on ws://127.0.0.1:9222/devtools/browser/

@AndreaCardaci in the documentation Load a URL in a separate context mentions that when using Google Chrome in Headless Mode:

The usual example code is run in a new disposable tab in a separated context (think of it as incognito profiles) each time.

As inorder to fetch the browser version since Chrome 62 the browser target URL is generated at runtime and can be obtained via the /json/version endpoint and fallback to /devtools/browser if not present.

Here is the relevant code:

const CDP = require('chrome-remote-interface');

async function doInNewContext(action) {
    // fetch the browser version (since Chrome 62 the browser target URL is
    // generated at runtime and can be obtained via the '/json/version'
    // endpoint, fallback to '/devtools/browser' if not present)
    const {webSocketDebuggerUrl} = await CDP.Version();
    // connect to the DevTools special target
    const browser = await CDP({
        target: webSocketDebuggerUrl || 'ws://localhost:9222/devtools/browser'
    });
    // create a new context
    const {Target} = browser;
    const {browserContextId} = await Target.createBrowserContext();
    const {targetId} = await Target.createTarget({
        url: 'about:blank',
        browserContextId
    });
    // connct to the new context
    const client = await CDP({target: targetId});
    // perform user actions on it
    try {
        await action(client);
    } finally {
        // cleanup
        await Target.closeTarget({targetId});
        await browser.close();
    }
}

// this basically is the usual example
async function example(client) {
    // extract domains
    const {Network, Page} = client;
    // setup handlers
    Network.requestWillBeSent((params) => {
        console.log(params.request.url);
    });
    // enable events then start!
    await Promise.all([Network.enable(), Page.enable()]);
    await Page.navigate({url: 'https://github.com'});
    await Page.loadEventFired();
}

doInNewContext(example);

Moreover as per Obtain the browser target URL (ws://localhost:9222/devtools/browser/...) programmatically the endpoint is available over http://127.0.0.1:9222/json/version in a webSocketDebuggerUrl field. So alternatively, if you are starting chrome with the option --remote-debugging-port=0, both port and endpoint are written to the DevToolsAcivePort file in browser profile folder.

Conclusion

This error doesn't impact your @Test and you can ignore the error for the time being.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!