首页 文章

如何在Python中使用Selenium打开chrome开发人员控制台?

提问于
浏览
3

我正在尝试使用selenium webdriver在chrome中打开开发人员控制台 . 我在做

从selenium导入webdriver从selenium.webdriver.common导入action_chains,键

...

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

但它没有开放开发者控制台 . 我尝试了其他键(只需键入一些键击,控制选择一些元素),然后它们正在工作 .

我正在使用ChromeDriver

4 回答

  • 2

    只有当您处于绝望状态并且您的操作系统是Windows时,您才可以通过向Python代码添加AutoHotKey脚本来执行此操作 . 你可以从here下载AutoHK

    安装AutoHK . 然后在记事本中创建新脚本:只需输入一个短字符串

    Send ^+J

    并将其另存为 script.ahk . 这些操作需要2-3分钟 . 然后在你的代码中调用它

    browser.get("https://www.facebook.com/groups/GNexus5/")
    import os
    os.system("path_to_script.ahk/script.ahk")
    

    这会工作:)

  • -1

    在启动chrome时,告诉selenium包含一个''auto-open-devtools-for-tabs'',这是一个使用nightwatch配置的示例:

    ...
    
    chrome: {
      desiredCapabilities: {
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true,
        chromeOptions: {
          'args': ['incognito', 'disable-extensions', 'auto-open-devtools-for-tabs']
        }
      }
    },
    ...
    
  • -1

    正如Andersson所说,无法通过ChromeDriver中的send_keys打开控制台 . 我的任务没有打开控制台就完成了 - 使用

    browser.execute_script(javascipt_command_for_the_console)

    http://selenium-python.readthedocs.io/api.html?highlight=execute_script#selenium.webdriver.remote.webdriver.WebDriver.execute_script

  • 0

    driver.find_element_by_xpath(<any element_name on the webpage>).send_keys(Keys.F12)

    这将直接打开开发人员控制台!

    您还可以使用其他find_by方法 .

相关问题