简体   繁体   中英

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

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']
    }
  }
},
...

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

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

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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