travis bickle
travis bickle

Reputation: 227

How to open chrome developer console using Selenium in Python?

I am trying to open developer console in chrome using selenium webdriver. I am doing

from selenium import webdriver

from selenium.webdriver.common import action_chains, keys

...

browser = webdriver.Chrome(executable_path="C:\chrm\chromedriver.exe") browser.get("https://www.facebook.com/groups/GNexus5/")

...

action = action_chains.ActionChains(browser)

action.send_keys(keys.Keys.CONTROL+keys.Keys.SHIFT+'j')

action.perform()

But it is not opening up developer console. I have tried other keys (just typing some key-strokes, control-selecting some element) and they are working.

I am using ChromeDriver

Upvotes: 6

Views: 21835

Answers (6)

Xoron
Xoron

Reputation: 11

Selenium 4.1.3 Driver Version: ChromeDriver Python 3 programming language

In order to open the developer toolbar in the browser, you need to add an option with the value options.add_argument("auto-open-devtools-for-tabs"). Such an argument, so you change the initial launch settings of chromedriver and open the browser together with the "Developer Panel" call

options = webdriver.ChromeOptions()
options.add_argument("auto-open-devtools-for-tabs") # <---

driver = webdriver.Chrome(options=options)

Upvotes: 1

yuyu5
yuyu5

Reputation: 474

While it's not opening the dev-tools pane itself, I would refer you to this answer which explains how to run commands specific to the dev-tools console.

If you really need to open the pane itself, there's probably an answer in the dev-tools documentation.

FYI You need Selenium version 4.0.0.b3 to do these actions. Dev-tools isn't supported in the stable release.

Upvotes: 0

Jortega
Jortega

Reputation: 3790

With pyautogui you can do the keyboard press and open the console in the tab you have in foucs.

    import pyautogui
    pyautogui.keyDown('ctrl')
    pyautogui.keyDown('shift')
    pyautogui.press('j')
    pyautogui.keyUp('ctrl')
    pyautogui.keyUp('shift')

Upvotes: 4

Manel Clos
Manel Clos

Reputation: 1835

Tell selenium to include a ''auto-open-devtools-for-tabs'' when launching chrome, here is an example using nightwatch configuration:

...

chrome: {
  desiredCapabilities: {
    browserName: 'chrome',
    javascriptEnabled: true,
    acceptSslCerts: true,
    chromeOptions: {
      'args': ['incognito', 'disable-extensions', 'auto-open-devtools-for-tabs']
    }
  }
},
...

Upvotes: 5

KarunaL
KarunaL

Reputation: 17

driver.find_element_by_xpath(<any element_name on the webpage>).send_keys(Keys.F12)

This opens the developer console directly!

You can also use other find_by methods.

Upvotes: -3

Andersson
Andersson

Reputation: 52665

Only if you are in desperate and your OS is Windows, you can simply do this with adding AutoHotKey script to Python code. You can download AutoHK from here

Install AutoHK. Then you create new script in notepad: just put one short string

Send ^+J

and save it as script.ahk. These actions will takes 2-3 minutes. Then call it in your code

browser.get("https://www.facebook.com/groups/GNexus5/")
import os
os.system("path_to_script.ahk/script.ahk")

and this gonna work :)

Upvotes: 2

Related Questions