selenium 如何在Firefox中打开控制台PYTHON SENSE?

inn6fuwd  于 2022-11-10  发布在  Python
关注(0)|答案(6)|浏览(173)

我正在尝试通过Selify和Python打开Firefox控制台。如何使用PYTHON SELENCE打开Firefox控制台?可以把钥匙寄给司机或类似的东西吗?

k10s72fa

k10s72fa1#

我知道这是相对陈旧的,但我最近遇到了这个问题。我通过传递浏览器进程参数“-DevTools”让Firefox自动打开DevTools。
Selify: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()
h5qlskok

h5qlskok2#

尝试使用send_keys函数模拟与“常规”Firefox窗口相同的过程:

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + 'k')
mdfafbf1

mdfafbf13#

我没有安装Firebug,这可以在MacOS上运行:

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + Keys.ALT + 'k')
cbjzeqam

cbjzeqam4#

这是可行的:
ActionChains(driver).key_down(Keys.F12).key_up(Keys.F12).perform()
至少未安装FireBug:)

a7qyws3x

a7qyws3x5#

在Firefox 60+中,您需要使用Chrome上下文(CONTEXT_Chrome)并选择一些UI元素来向控制台发送按键,这个示例向您展示了如何使用控制台中的GCLI命令,使用Chrome上下文和tabBrowser-Tabs UI元素来发出击键

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()
rdrgkggo

rdrgkggo6#

在Firefox上访问开发人员控制台

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.action_chains import ActionChains

driver_options = Options()
driver = webdriver.Firefox(
        options = driver_options,
        executable_path = "c:\webdrivers\geckodriver.exe")

actions = ActionChains(driver)
actions.send_keys(Keys.COMMAND + Keys.ALT + 'k')

相关问题