8

Im trying to open firefox console through Selenium with Python. How can I open firefox console with python selenium? Is it possible to send keys to the driver or something like that?

patricmj
  • 365
  • 2
  • 3
  • 18

6 Answers6

7

I know this is relatively old but I ran into this issue recently. I got firefox to automatically open devtools by passing in a browser process argument "-devtools".

Selenium: 3.14 geckodriver: 0.21.0 firefox: 61.0.1

  from __future__ import print_function

  from datetime import datetime
  import logging
  import os

  from selenium import webdriver
  from selenium.webdriver.firefox.options import Options as FirefoxOptions

  def before_scenario(context, scenario):
    logging.info("RUNNING: " + scenario.name)
    print("Browser Test starting.\n")

    options = FirefoxOptions()
    options.log.level = "trace"
    options.add_argument("-devtools")

    if 'headless' in os.environ and os.environ['headless'] == '1':
         options.headless = True

    context.driver = webdriver.Firefox(firefox_options=options)


    context.driver.maximize_window()
darkknight51
  • 71
  • 1
  • 2
  • Here is a list of all of the possible command line arguments for FireFox. https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options – darkknight51 Aug 07 '18 at 20:52
  • `webdriver.Firefox(firefox_options=options)` -> `webdriver.Firefox(options=options)`. – sergzach Aug 24 '22 at 13:38
  • What are the values of the context and scenario objects that need to be passed into as arguments to call the function above? Can someone please provide a usage example... – Rich Lysakowski PhD Nov 01 '22 at 06:51
5

Try to simulate the same procedure as a "regular" firefox window using the send_keys function:

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + 'k')
Javitronxo
  • 817
  • 6
  • 9
  • That works aswell, and that will work if you got firebug installed. – patricmj Nov 05 '15 at 14:37
  • 1
    Even better: WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_tag_name("body")).send_keys(Keys.CONTROL + Keys.SHIFT + 'k') – patricmj Nov 05 '15 at 14:39
  • In general it is a good practice to use the WebDriver waits, I always set the implicit wait so I don't have to use explicit waits for each element as you have pointed out. After create the driver element add: `driver.implicitly_wait(15)` – Javitronxo Nov 05 '15 at 14:54
  • above solution is no more working for - VS 2017 , Selenium v 3.12.1 ,C# , Firefox V 60.0.2 , Chrome V 66 , Nunit v3.10.1 , Gecko Driver v 20.1 , chrome driver v 2.4 – Mike ASP Jun 12 '18 at 23:03
2

I have no firebug installed, this works on Macos:

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + Keys.ALT + 'k')
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
  • Command + alt + k / Ctrl + shift + k will always work. The thing is if firebug is installed it replaces the F12 button to open firebug, and not the developer tools. – patricmj Nov 05 '15 at 17:12
  • above solution is no more working for - VS 2017 , Selenium v 3.12.1 ,C# , Firefox V 60.0.2 , Chrome V 66 , Nunit v3.10.1 , Gecko Driver v 20.1 , chrome driver v 2.4 – Mike ASP Jun 12 '18 at 23:03
  • @MikeASP what works then for the versions you mentioned? – iDrwish Jul 31 '18 at 11:30
1

This works:

ActionChains(driver).key_down(Keys.F12).key_up(Keys.F12).perform()

Without firebug installed at least :)

patricmj
  • 365
  • 2
  • 3
  • 18
1

In Firefox 60+ you need to use the chrome context (CONTEXT_CHROME) and select some UI element to send keys to the console, this example shows you how to use a GCLI command from console using chrome context and tabbrowser-tabs UI element to issue the keystrokes

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.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

import time

profile = FirefoxProfile()
# Allow autoplay
profile.set_preference("media.autoplay.default", 0)
cap = DesiredCapabilities.FIREFOX
options = Options()
options.headless = True
webdriver = Firefox(firefox_profile=profile, capabilities=cap, options=options)
webdriver.get("https://www.youtube.com/watch?v=EzKkl64rRbM")
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')
        time.sleep(5)
        console.send_keys(':screenshot --full-page' + Keys.ENTER)
        console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
except:
    pass
webdriver.quit()
lukss12
  • 106
  • 4
0

Accessing Developer console on Firefox

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains

driver_options = Options()
driver = webdriver.Firefox(
        options = driver_options,
        executable_path = "c:\webdrivers\geckodriver.exe")

actions = ActionChains(driver)
actions.send_keys(Keys.COMMAND + Keys.ALT + 'k')
Rich Lysakowski PhD
  • 2,702
  • 31
  • 44
  • after running this in python on windows 10 I get error, Traceback (most recent call last): File XXX, line 5, in driver_options = Options() NameError: name 'Options' is not defined – Ray Troy Mar 06 '20 at 17:02