硒能打开遥控无头镀铬的调试页吗?

时间:2017-08-30 13:47:31

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

  
      
  • 平台:

         
        
    • 计算机A(网格中心):Ubuntu 14.04
    •   
    • 计算机B(网格节点):Ubuntu 16.04
    •   
  •   
  • python + Selenium Grid +无头Chrome

  •   

我做了什么

我正在使用Selenium Grid - 在计算机A中设置了一个Hub,在计算机B中设置了一个节点。我可以通过下面的python代码打开计算机B的无头Chrome:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

def open_remote_headless_chrome():
    # parameters
    head_port = 9222

    # config options
    options = webdriver.ChromeOptions()
    options.binary_location = '/usr/bin/google-chrome-stable'
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    # options.add_argument('--remote-debugging-port=' + str(head_port))
    # -> If I uncomment the line above, it will cause error

    # open remote headless Chrome
    driver = webdriver.Remote(
        desired_capabilities=options.to_capabilities()
    )

    # bing 'headless'
    driver.get("http://www.sogou.com")
    elem_search_input = driver.find_element_by_xpath("//input[@id='query']")
    elem_search_input.send_keys("headless")
    elem_search_input.send_keys(Keys.ENTER)

    # To Ensure that I've successfully opened the headless Chrome
    time.sleep(1)  # wait till search result page loaded
    print(driver.title)  # was the right title - "headless - 搜狗搜索"

    driver.quit()

但是当我向--remote-debugging-port=9222添加options时,终端中运行的进程将冻结并最终抛出错误。

  

(我今天晚些时候会发布终端信息......)

我找到了什么

计算机B上的

netstat -a | grep localhost | grep ESTABLISHED列出了以下信息:

tcp        0      0 localhost:36976         localhost:socks         ESTABLISHED
tcp        0      0 localhost:46056         localhost:12970         ESTABLISHED
tcp        0      0 localhost:55144         123.126.51.32:https     ESTABLISHED
tcp        0      0 localhost:55918         localhost:24800         ESTABLISHED
tcp        0      0 localhost:36978         localhost:socks         ESTABLISHED
tcp        0      0 localhost:35700         localhost:socks         ESTABLISHED
tcp        0      0 localhost:12970         localhost:46056         ESTABLISHED
tcp        0      0 localhost:46068         localhost:12970         ESTABLISHED
tcp        0      0 localhost:12970         localhost:45556         ESTABLISHED
tcp        0      0 localhost:45554         localhost:12970         ESTABLISHED
tcp        0      0 localhost:45556         localhost:12970         ESTABLISHED
tcp        0      0 localhost:12970         localhost:46068         ESTABLISHED
tcp        0      0 localhost:36974         localhost:socks         ESTABLISHED
tcp        0      0 localhost:12970         localhost:45554         ESTABLISHED
tcp        0      0 localhost:34520         localhost:19080         ESTABLISHED
tcp6       0      0 localhost:50004         localhost:4444          ESTABLISHED
tcp6       0      0 localhost:59614         122.97.254.22:http      ESTABLISHED
tcp6       0      0 localhost:5555          localhost:43993         ESTABLISHED
tcp6       0      0 localhost:19080         localhost:34520         ESTABLISHED
udp  
  

我的尝试证明计算机B上的相关端口始终以12开头,如12500,12341,12970 ......

当我在计算机B上访问localhost:12970时,我以为我找到了迄今为​​止我一直在寻找的调试页面,但它实际上与正常的不同:

<!-- body of site localhost:12970 -->
<div id="caption">Inspectable WebContents</div>
<div id="items">
    <p>
        <div title="The tab already has active debugging session">
            <div>headless - 搜狗搜索</div>
        </div>
    </p>
</div>

虽然'正常'就像:

<!-- start normally in command line by `chrome --headless --disable-gpu --remote-debugging-port=9222 http://www.sogou.com/` -->
<div id="caption">Inspectable WebContents</div>
<div id="items">
    <p>
        <a href="https://chrome-devtools-frontend.appspot.com/serve_file/@3cf8514bb1239453fd15ff1f7efee389ac9df8ba/inspector.html?ws=localhost:9222/devtools/page/289a78c9-d480-4c90-9469-c69e2d8133a4&remoteFrontend=true" title="搜狗搜索引擎 - 上网从搜狗开始">
            <div>搜狗搜索引擎 - 上网从搜狗开始</div>
        </a>
        <!-- Note that the div tag wrapped in an anchor! -->
    </p>
</div>

我想要什么

我能为选项添加调试端口吗?如果能够,我能够打开Remote() FireFox(因为无法在Chrome中打开调试链接)到远程访问调试页面(在我的情况下,其网址为{ {1}}),用于调试无头Chrome。

THX

有什么想法吗?谢谢!

0 个答案:

没有答案