1

The title says it all: I am wondering whether it is possible to interact with the Firefox console upon starting Firefox in headless mode.

More generally, I'd settle for some way of accessing it programmatically, in scripts.

What I've tried:

So far I've been playing with the Javascript bindings to Selenium without success:

Starting Firefox with the -devtools option from Selenium does opn the dev tools, but I then cannot send key combinations that will switch me to the actual console, or in fact interact from my .js script with the open devtools window in any way.


Edit

In response to the first comment below: this answer does not seem to help. The console is not opened when I send CTRL+SHIFT+k to the body tag of google.com:

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');

var options = new firefox.Options();

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(options)
    .build();

(async function(){
    await driver.get('https://google.com');
    var bdy = await driver.findElement(By.id('gsr'));
    await bdy.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');    
})();

This opens the page (google.com) and returns no errors, but there's no console anywhere.

For good measure: sending just inpt.Key.SHIFT + 'k' does enter a capital 'K' in the Google search field, so I know the keys are referenced correctly.

Also, sending just 'k' enters a small 'k' in the search field. It's only the three-key combo that does not work.

2nd edit:

I take it back: the newer answer does work, precisely as-is (I switched to Python from node).

grobber
  • 1,083
  • 1
  • 9
  • 20

1 Answers1

0

The comment below by Karthik does resolve the matter, but I would like to summarize here and document working solutions that automate Firefox-Web-Console access.

The point of the answer I linked to above (in my 2nd edit) is that in order to have full access to the Firefox browser key controls one must

  • first switch Firefox context to chrome (from the default content context)
  • direct the automated browser driver to locate the element carrying id tabbrowser-tabs
  • send the key combo (in this case Ctrl+Shift+k) to that element.

Concrete working solutions:

Python

The script is

from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys

import time

options = Options()
webdriver = Firefox(options=options)
webdriver.get("https://google.com")
try:
    time.sleep(3)
    with webdriver.context(webdriver.CONTEXT_CHROME):
        console = webdriver.find_element(By.ID, "tabbrowser-tabs")
        console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
                
except:
    pass

Run with python <path-to-script> it opens a Firefox window displaying google.com and the console at the bottom.

Javascript

Here the full script is

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');

var options = new firefox.Options();

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .setFirefoxOptions(options)
    .build();

(async function(){
    await driver.get('https://google.com');
    await driver.setContext("chrome");

    var tabs = await driver.findElement(By.id('tabbrowser-tabs'));
    await tabs.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');
    
})();

Run with node <path-to-script> it achieves the same effect as above: a Firefox window open on google.com, with the console open at the bottom.

grobber
  • 1,083
  • 1
  • 9
  • 20