本文介绍了DevTools使用Selenium和Python在ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5上侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python +硒来实现Web浏览器自动化,并收到此错误.

I am using python + selenium for web browser automation and getting this error.

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

当程序到达代码的这一部分时,就会出现问题:-

The problem arises when the program reaches this part of code:-

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)

它也没有登录控制台.谁能说出问题所在吗?

Its also not logging in console. Can anyone tell whats the problem?

推荐答案

DevTools在ws://127.0.0.1:9222/devtools/browser/

上监听

@AndreaCardaci在文档 在单独的上下文中加载URL 提到在无头模式下使用 Google Chrome

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

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.

以下是相关代码:

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

此外,根据获得浏览器目标URL(ws://以编程方式 localhost:9222/devtools/browser/...),可以在 webSocketDebuggerUrl 字段中的http://127.0.0.1:9222/json/version上使用该端点.因此,或者,如果您使用选项--remote-debugging-port=0启动chrome,则将端口端点都写入浏览器配置文件文件夹中的 DevToolsAcivePort 文件中

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.

此错误不会影响您的@Test,您暂时可以忽略该错误.

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

这篇关于DevTools使用Selenium和Python在ws://127.0.0.1:57671/devtools/browser/8a586f7c-5f2c-4d10-8174-7a7bf50e49b5上侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:24