DevTools 使用 Selenium 和 Python 监听 ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5

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

我正在使用 python + selenium 进行 Web 浏览器自动化并收到此错误。

DevTools listening on ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5
[5096:1196:0909/183254.362:ERROR:mf_helpers.cc(14)] Error in dxva_video_decode_accelerator_win.cc on line 517

当程序运行到这段代码时出现问题:-

def send_comments(driver):
    add_comments = driver.find_elements_by_class_name('add') 
    comments = driver.find_elements_by_xpath("//form[@class='addCommentexpand']//textarea[contains(@placeholder,'Add a comment')]") 
    submit_comments = driver.find_elements_by_xpath("//button[text()='Comment']")  
    i = 0
    for add, comment, submit in zip(add_comments, comments, submit_comments):
        print("comment begins")
        add.click()
        print("+add comment clicked")
        comment.click()
        print("comment textbox clicked")
        comment.send_comments("Amazing Art")
        print("text typed")
        submit.click()
        print("comment submited")
        i += 1
        if i > 5:
            driver.close()

send_comments(driver)

它也没有登录控制台。谁能告诉我这是怎么回事?

DevTools 监听 ws://127.0.0.1:9222/devtools/browser/

@AndreaCardaci 在文档 Load a URL in a separate context 中提到在无头模式下使用 Google Chrome 时 :

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

为了获取自 Chrome 62 以来的浏览器版本,浏览器目标 URL 是在运行时生成的,可以通过 /json/version 端点和回退到 /devtools/browser 如果不存在。

相关代码如下:

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);

此外,根据 Obtain the browser target URL (ws://localhost:9222/devtools/browser/...) programmatically,端点在 webSocketDebuggerUrl 字段中的 http://127.0.0.1:9222/json/version 上可用。因此,或者,如果您使用选项 --remote-debugging-port=0 启动 chrome,portendpoint 都会写入 DevToolsAcivePort 浏览器配置文件夹中的文件。

结论

这个错误不会影响你的@Test,你可以暂时忽略这个错误。

基于 hide chromeDriver console in python

中的雄鸡

在您的 Python 文件夹中,找到并编辑此文件:

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

通过以这种方式添加创建标志来编辑 Start() 函数: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

它非常适合我使用 python3.7 和 selenium 3.141.0

试试这个:

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])

这是作品:

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
...
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--log-level=3")
browser = webdriver.Chrome(chromedriver_path, chrome_options=chrome_options)

对于无法修改包源代码的其他人(因为它需要在 pip 安装后才能工作!!),感谢@Denis,这实际上对我有用:

chrome_options.add_argument("--log-level=3")

它删除了恼人的 JQMITIGATE 日志..

(我无法发表评论或其他任何内容,这就是我写这个答案的原因..)