Firefox Web Console是否可以无头模式访问? [英] Is the Firefox Web Console accessible in headless mode?

查看:183
本文介绍了Firefox Web Console是否可以无头模式访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切:我想知道是否可以与Firefox交互无头模式中启动Firefox时,noreferrer>控制台 a>.

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.

我尝试过的事情:

到目前为止,我一直在使用将Java绑定绑定到没有成功:

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

使用Selenium中的 -devtools选项来启动Firefox确实可以开发工具,但随后我无法发送组合键,这会将我切换到实际控制台,或者实际上以任何方式从我的.js脚本与打开的devtools窗口进行交互.

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.

修改

针对下面的第一条评论:此答案似乎无济于事.将CTRL+SHIFT+k发送到google.combody标记时,控制台未打开:

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');    
})();

这将打开页面(google.com),并且不返回任何错误,但是任何地方都没有控制台.

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

出于良好的考虑:仅发送inpt.Key.SHIFT + 'k' 在Google搜索字段中输入大写的"K",这样我就知道密钥正确引用了.

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.

此外,仅发送'k'在搜索字段中输入一个小"k".只是三键组合不起作用.

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

第二次修改:

我收回了它:新答案确实可以正常工作(我切换到node中的"https://pythonspot.com/selenium-webdriver/" rel ="nofollow noreferrer"> Python ).

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

推荐答案

Karthik 的以下评论确实解决了此问题,但我想在此进行总结,并介绍可自动执行Firefox-Web-Console访问的可行解决方案.

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.

答案的要点我链接到上面(在我的第二次编辑中)是,要拥有对Firefox浏览器关键控件的完全访问权限,必须

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 上下文chrome(从默认的content上下文开始)
  • 引导自动浏览器驱动程序找到带有id tabbrowser-tabs
  • 的元素
  • 将组合键(在本例中为Ctrl+Shift+k)发送到该元素.
  • 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.

脚本是

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

使用python <path-to-script>运行,它将打开一个Firefox窗口,其中显示google.com和位于底部的控制台.

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

完整脚本是

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');
    
})();

使用node <path-to-script>运行,可以达到与上述相同的效果:在google.com上打开Firefox窗口,在底部打开控制台.

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.

这篇关于Firefox Web Console是否可以无头模式访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆