6

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

6 Answers 6

5

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']
    }
  }
},
...
Sign up to request clarification or add additional context in comments.

Comments

4

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')

Comments

2

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 :)

2 Comments

I don't mean that. But I've tried send_keys as well as key_down and no result in both cases. Also tried in Firefox browser and it works. So it seams that ChromeDriver doesn't support keys combinations and if in not a mandatory for you to use Chrome you can just replace one line in your code with browser = webdriver.Firefox()
@Andersson the question is about how to open the Chrome developer console, so using Firefox obviously isn't a viable workaround.
1

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)

3 Comments

Please add some kind of explanation to your solution
@vahid-tajari added an explanation
For some one if you are using "options.add_experimental_option("detach", True)", You may meet current url is "devtools://devtools/bundled/devtools_app.html" when you re-connect to the opened browser, use "driver.switch_to.window(driver.window_handles[0])" CAN help switch to the correct tab.
0

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.

Comments

-3

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.

2 Comments

This doesn't work for me. I tried driver.find_element_by_tag_name('body').send_keys(Keys.F12) but nothing happens.
Worked for me using an Xpath locator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.