英文:
Simulate a keyboard shortcut using selenium to activate a chrome extension
问题
I have a chrome extension which when activated (clicked or keyboard-shortcut ) parses some data from the given site and sends it to my server. I need to automate this task to run daily at a specific time most preferably in headless mode.
The extension was loaded using a .crx file. The extension can be activated by clicking the extension icon or by pressing the alt+p keyboard shortcut.alt+p. Trying to simulate the keypress using Selenium doesn't seem to work, but other keyboard shortcuts, like ctrl+A, do.
from selenium.webdriver.common.action_chains import ActionChains
keyAction = ActionChains(driver)
keyAction.key_down(Keys.ALT).send_keys("p").key_up(Keys.ALT)
How to effectively simulate this shortcut?
Is there any other way I can activate this extension?
英文:
I have a chrome extension which when activated (clicked or keyboard-shortcut ) parses some data from the given site and sends it to my server. I need to automate this task to run daily at a specific time most preferably in headless mode.
The extension was loaded using a .crx file. The extension can be activated by clicking the extension icon or by pressing the alt+p keyboard shortcut.alt+p. Trying to simulate the keypress using Selenium doesn't seem to work, but other keyboard shortcuts, like ctrl+A, do.
from selenium.webdriver.common.action_chains import ActionChains
keyAction = ActionChains(driver)
keyAction.key_down(Keys.ALT).send_keys("p").key_up(Keys.ALT)
How to effectively simulate this shortcut?
Is there any other way I can activate this extension?
答案1
得分: 1
你可以在启动Chrome浏览器后,使用Selenium中的Chrome DevTools Protocol(CDP)来激活Chrome扩展。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Chrome WebDriver可执行文件的路径
webdriver_path = '/path/to/chromedriver'
# 扩展CRX文件的路径
extension_path = '/path/to/extension.crx'
# 创建一个ChromeOptions对象
options = Options()
# 将扩展路径添加到ChromeOptions
options.add_argument(f'--load-extension={extension_path}')
# 创建一个ChromeDriverService实例
service = Service(webdriver_path)
# 使用ChromeDriverService和ChromeOptions创建一个WebDriver
driver = webdriver.Chrome(service=service, options=options)
# 使用Chrome DevTools Protocol激活扩展
dev_tools = driver.execute_cdp_cmd('Browser.enable', {})
driver.execute_cdp_cmd('Browser.activateTarget', {'targetId': driver.current_url})
# 现在,您可以使用驱动程序与激活的扩展进行交互
# 例如,您可以打开一个URL并与该页面上的扩展进行交互
driver.get('https://example.com')
通过在ChromeOptions中添加--load-extension参数,WebDriver将使用加载了指定扩展的Chrome启动。然后,使用Chrome DevTools Protocol,您可以通过执行Browser.activateTarget命令来激活扩展。
英文:
You can utilize the Chrome DevTools Protocol (CDP) in Selenium to activate a Chrome extension after launching the Chrome browser.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Path to your Chrome WebDriver executable
webdriver_path = '/path/to/chromedriver'
# Path to the extension CRX file
extension_path = '/path/to/extension.crx'
# Create a ChromeOptions object
options = Options()
# Add the extension path to the ChromeOptions
options.add_argument(f'--load-extension={extension_path}')
# Create a ChromeDriverService instance
service = Service(webdriver_path)
# Create a WebDriver with the ChromeDriverService and ChromeOptions
driver = webdriver.Chrome(service=service, options=options)
# Activate the extension using the Chrome DevTools Protocol
dev_tools = driver.execute_cdp_cmd('Browser.enable', {})
driver.execute_cdp_cmd('Browser.activateTarget', {'targetId': driver.current_url})
# Now you can use the driver to interact with the activated extension
# For example, you can open a URL and interact with the extension on that page
driver.get('https://example.com')
By adding the --load-extension argument to the ChromeOptions, the WebDriver will launch Chrome with the specified extension loaded. Then, using the Chrome DevTools Protocol, you can activate the extension by executing the Browser.activateTarget command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论