Home > Blockchain >  How do I open the DevTools in google Chrome using Selenium python?
How do I open the DevTools in google Chrome using Selenium python?

Time:01-13

Is this even possible as I saw one other post about this topic and its solution was to use pyautogui but I cant do that because I need the keys to only register for the chromedriver.

I have tried the typical

driver.find_element(By.CLASS_NAME,"body").send_keys(Keys.F12)

but with no avail and I had also tried

driver.find_element(By.CLASS_NAME,"body").send_keys(Keys.CONTROL Keys.SHIFT "i")

and this also did not work as well.

CodePudding user response:

There are some other workarounds depending on what your goal is.

If you need to open devtools to handle requests I would recommend using Selenium Wire

CodePudding user response:

It is possible to send keyboard shortcuts to the browser using Selenium and WebDriver. However, the method you're trying to use (driver.find_element(By.CLASS_NAME, "body").send_keys(Keys.F12)) is not the correct way to do it.

Instead, you should use the ActionChains class to simulate keyboard events. Here's an example of how you can send the F12 key to the browser.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


# ...

actions = ActionChains(driver)
actions.send_keys(Keys.F12)
actions.perform()

You can also use the ActionChains class to send the key combination you're trying to use:

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL)
actions.key_down(Keys.SHIFT)
actions.send_keys("i")
actions.key_up(Keys.CONTROL)
actions.key_up(Keys.SHIFT)
actions.perform()

It's also important to note that you may need to switch the focus of your webdriver to the browser window for the key events to be registered correctly.

  • Related