问题描述
我可以通过 Selenium 打开 Chrome,但无法模拟按键操作(特别是 F12,因为我想打开 Inspect 并最终使用移动浏览器 .
要打开 google-chrome-devtools
即 chrome-browser-console
,您必须使用 ChromeOptions
类来添加参数--auto-open-devtools-for-tabs
参数如下:
代码块:
from selenium import webdriver选项 = webdriver.ChromeOptions()options.add_argument("开始最大化")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:UtilityBrowserDriverschromedriver.exe')driver.get("https://selenium.dev/documentation/en/")打印(驱动程序.标题)
控制台输出:
Selenium 浏览器自动化项目 :: Selenium 文档
浏览器控制台快照:
您可以找到相关的基于 java 的讨论在如何通过Selenium打开Chrome浏览器控制台?一个>
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.
Simulating the key press for F12 resembles to opening the .
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:UtilityBrowserDriverschromedriver.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 上打开检查(按 F12)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!