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

时间:2018-09-09 14:44:06

标签: python selenium selenium-webdriver selenium-chromedriver google-chrome-headless

我正在使用python +硒来实现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)

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

4 个答案:

答案 0 :(得分:0)

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

文档Load a URL in a separate context中的

@AndreaCardaci提到在无头模式下使用 Google Chrome

  

通常的示例代码每次都在单独的上下文中的新的一次性选项卡中运行(认为它是隐身配置文件)。

为了从Chrome 62开始获取浏览器版本,浏览器目标URL是在运行时生成的,可以通过 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".view.activity.ActivityShowRent" android:theme="@style/AppThemeFullScreen" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layoutDirection="rtl" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="@color/colorPrimaryDark" app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/show_detail_media_height" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.7" android:id="@+id/lyt_game_info_head" android:fitsSystemWindows="true"> <VideoView android:id="@+id/vdo_game" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_marginTop="0dp" android:scaleType="fitXY" android:fitsSystemWindows="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginBottom="30dp"/> <ImageView android:id="@+id/img_game" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:fitsSystemWindows="true" android:scaleType="fitXY" android:src="@drawable/test" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginBottom="30dp" android:visibility="gone"/> <ImageView android:id="@+id/img_game_cover" android:layout_width="@dimen/item_list_games_image_width" android:layout_height="@dimen/item_list_games_image_height" android:layout_alignBottom="@+id/vdo_game" android:layout_alignParentEnd="true" android:layout_marginBottom="-30dp" android:layout_marginEnd="20dp" android:contentDescription="TODO" android:src="@drawable/test"/> <TextView android:id="@+id/txt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/txt_console" android:layout_alignParentStart="true" android:padding="5dp" android:textStyle="bold" android:text="test1" android:textSize="@dimen/font_size_medium" android:textColor="@color/main_text_color" android:background="#8f000000" android:layout_margin="3dp"/> <TextView android:id="@+id/txt_console" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/txt_genres" android:layout_alignParentStart="true" android:padding="5dp" android:text="test2" android:textColor="@color/main_text_color" android:textSize="@dimen/font_size_small" android:background="#8f000000" android:layout_margin="3dp"/> <TextView android:id="@+id/txt_genres" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/vdo_game" android:layout_alignParentStart="true" android:background="#8f000000" android:padding="5dp" android:text="test3" android:layout_marginStart="3dp" android:textColor="@color/main_text_color" android:textSize="@dimen/font_size_small"/> <TextView android:id="@+id/txt_release_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:padding="5dp" android:text="test4" android:textColor="@color/gray_text_color" android:textSize="@dimen/font_size_small"/> </RelativeLayout> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_behavior=".view.ToolbarBackgroundAlphaBehavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:layout_width="0sp" android:layout_height="match_parent" android:layout_weight="1"> <TextView android:id="@+id/txt_page_name" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="right" android:gravity="center" android:text="page name" android:textColor="#ffffff" android:textSize="14dp"/> </LinearLayout> <LinearLayout android:layout_width="48sp" android:layout_height="48sp" android:layout_gravity="center" android:layout_marginRight="10sp" android:gravity="center"> <ImageView android:id="@+id/img_back" android:layout_width="42dp" android:layout_height="42dp" android:layout_gravity="center_vertical" android:background="@drawable/back_buttons_ripple_effect" android:padding="10sp" android:scaleType="fitCenter" android:src="@drawable/ic_back"/> </LinearLayout> </LinearLayout> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:fillViewport="true" android:fitsSystemWindows="true" android:clipToPadding="false" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/colorPrimary"> <Button android:layout_width="match_parent" android:layout_height="48dp" android:layout_margin="20dp" android:background="@drawable/back_buttons" android:text="play" android:textColor="@color/colorPrimary" android:textSize="@dimen/font_size_very_large" android:textStyle="bold"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/main_text_color" android:padding="5dp" android:text="Lorem ipsum is a pseudo-Latin text used in web design, typography, layout, and printing in place of English to emphasise design elements over content. It's also called placeholder (or filler) text. It's a convenient tool for mock-ups. It helps to outline the visual elements of a document or presentation, eg typography, font, or layout. Lorem ipsum is mostly a part of a Latin text by the classical author and philosopher Cicero. Its words and letters have been changed by addition or removal, so to deliberately render its content nonsensical; it's not genuine, correct, or comprehensible Latin anymore. While lorem ipsum's still resembles classical Latin, it actually has no meaning whatsoever. As Cicero's text doesn't contain the letters K, W, or Z, alien to latin, these, and others are often inserted randomly to mimic the typographic appearence of European languages, as are digraphs not to be found in the original."/> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 端点获取并回退到 {{1} } (如果不存在)。

以下是相关代码:

/json/version

此外,根据Obtain the browser target URL (ws://localhost:9222/devtools/browser/...) programmatically,在 /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); 上可以使用端点。因此,或者,如果您使用选项http://127.0.0.1:9222/json/version启动chrome,则将端口端点都写入浏览器中的 DevToolsAcivePort 文件中个人资料文件夹。

结论

此错误不会影响您的webSocketDebuggerUrl,您可以暂时忽略该错误。

答案 1 :(得分:0)

基于hide chromeDriver console in python中的Chanticleer

在您的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和硒3.141.0对我来说非常合适

答案 2 :(得分:-1)

试试这个:

settings.py.

答案 3 :(得分:-1)

这是工作:

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)