简体   繁体   English

Firefox Web 控制台是否可以在无头模式下访问?

[英]Is the Firefox Web Console accessible in headless mode?

The title says it all: I am wondering whether it is possible to interact with the Firefox console upon starting Firefox in headless mode .标题说明了一切:我想知道是否可以在无头模式下启动 Firefox 时与 Firefox控制台进行交互。

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:到目前为止,我一直在玩Javascript 绑定Selenium但没有成功:

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.使用 Selenium 中的-devtools 选项启动 Firefox 确实会打开开发工具,但我无法发送将我切换到实际控制台的组合键,或者实际上无法从我的.js脚本与打开的 devtools window 以任何方式进行交互。


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 :当我将CTRL+SHIFT+k发送到google.combody标签时,控制台没有打开:

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.这将打开页面 ( google.com ) 并且没有返回任何错误,但是任何地方都没有控制台。

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.好的措施:仅发送inpt.Key.SHIFT + 'k'确实在 Google 搜索字段中输入了大写的 'K',所以我知道键被正确引用。

Also, sending just 'k' enters a small 'k' in the search field.此外,仅发送'k' k”会在搜索字段中输入一个小的“k”。 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 ).我收回:较新的答案确实有效,完全按原样(我从node切换到Python )。

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. Karthik下面的评论确实解决了这个问题,但我想在这里总结一下并记录自动化 Firefox-Web-Console 访问的工作解决方案。

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我上面链接的答案(在我的第二次编辑中)的要点是,为了完全访问 Firefox 浏览器键控件,必须

  • first switch Firefox context to chrome (from the default content context)首先将 Firefox 上下文切换到chrome (从默认content上下文)
  • direct the automated browser driver to locate the element carrying id tabbrowser-tabs指示自动浏览器驱动程序定位带有 id tabbrowser-tabs的元素
  • send the key combo (in this case Ctrl+Shift+k ) to that element.将组合键(在本例中为Ctrl+Shift+k )发送到该元素。

Concrete working solutions:具体工作方案:

Python 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.使用python <path-to-script>运行它会打开一个 Firefox window 显示google.com和底部的控制台。

Javascript 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.使用node <path-to-script>运行它实现与上面相同的效果: Firefox window 在google.com上打开,控制台在底部打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM