问题
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?
回答1:
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')
回答2:
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()
回答3:
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')
回答4:
This works:
ActionChains(driver).key_down(Keys.F12).key_up(Keys.F12).perform()
Without firebug installed at least :)
回答5:
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()
回答6:
Accessing Developer console on Firefox
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver_options = Options()
driver = webdriver.Firefox(
options=driver_options,
executable_path="geckodriver.exe")
actions = ActionChains(driver)
actions.send_keys(Keys.COMMAND + Keys.ALT + 'k')
来源:https://stackoverflow.com/questions/33546911/how-to-open-console-in-firefox-python-selenium