Can selenium open remote headless chrome's debugging page?

3.2k views Asked by At
  • Platform:

    • Computer A (Grid's Hub): Ubuntu 14.04
    • Computer B (Grid's Node): Ubuntu 16.04
  • python + Selenium Grid + headless Chrome

what i did

I'm using Selenium Grid - having set a Hub in Computer A and a Node in Computer B. And I'm able to open Computer B's headless Chrome by python codes below:

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

But when I add --remote-debugging-port=9222 to options ,the process running in terminal will freeze and throw errors finally.

(I will post terminal info here later today...)

what i found

netstat -a | grep localhost | grep ESTABLISHED on Computer B listed the following infomation:

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  

My trying proved that the related port on Computer B always starts with 12, like 12500, 12341, 12970...

When I visit localhost:12970 on Computer B, I thought I've found the debugging page I've been seeking so far, but it's in fact different from a normal one:

<!-- 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>

While a 'normal' one is like:

<!-- 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>

what i want

Am I able to add a debug port to options ? And if able, am I able to open a Remote() FireFox(since one can not open debug link still in Chrome) to access the debug page remotely (in my case, whose url is localhost:9222), which is for debugging headless Chrome.

thx

Any ideas? Thanks!

0

There are 0 answers