通过Selenium在Chrome上打开检查(按F12) [英] Opening inspect (pressing F12) on Chrome via Selenium

查看:1666
本文介绍了通过Selenium在Chrome上打开检查(按F12)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过Selenium打开Chrome,但是无法模拟按键(特别是F12,因为我想打开Inspect并最终使用移动浏览器

I am able to open Chrome via Selenium, but am unable to simulate a key press (specifically F12, since I want to open Inspect and eventually use the mobile browser Like so) While I'm able to do it manually i.e, open Chrome and press F12, I want to be able to automate this part using Selenium. My current code looks like this -

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome('/Users/amigo/Documents/pet_projects/selenium/chromedriver')
driver.get('https://www.google.com')
ActionChains(driver).send_keys(Keys.F12).perform()

尽管代码运行没有任何错误,但我看不到在浏览器中打开了检查.任何建议和帮助表示赞赏!先感谢您.

While the code runs without any errors, I do not see the inspect being opened on the browser. Any suggestions and help appreciated! Thank you in advance.

推荐答案

模拟F12的按键类似于打开.

Simulating the key press for F12 resembles to opening the .

要打开google-chrome-devtools,即chrome-browser-console,必须使用ChromeOptions类添加参数 --auto-open-devtools-for-tabs 参数,如下所示:

To open the google-chrome-devtools i.e. the chrome-browser-console you have to use the ChromeOptions class to add the argument --auto-open-devtools-for-tabs argument as follows:

  • 代码块:

  • Code Block:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument("--auto-open-devtools-for-tabs")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://selenium.dev/documentation/en/")
print(driver.title)

  • 控制台输出:

  • Console Output:

    The Selenium Browser Automation Project :: Documentation for Selenium
    

  • 浏览器控制台快照:

  • Browser Console Snapshot:

    您可以找到相关的的问题在如何通过Selenium打开Chrome浏览器控制台?

    这篇关于通过Selenium在Chrome上打开检查(按F12)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆