使用Selenium模拟键盘快捷键来激活Chrome扩展。

huangapple go评论100阅读模式
英文:

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.

  1. from selenium.webdriver.common.action_chains import ActionChains
  2. keyAction = ActionChains(driver)
  3. 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.

  1. from selenium.webdriver.common.action_chains import ActionChains
  2. keyAction = ActionChains(driver)
  3. 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扩展。

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. from selenium.webdriver.chrome.options import Options
  4. # Chrome WebDriver可执行文件的路径
  5. webdriver_path = '/path/to/chromedriver'
  6. # 扩展CRX文件的路径
  7. extension_path = '/path/to/extension.crx'
  8. # 创建一个ChromeOptions对象
  9. options = Options()
  10. # 将扩展路径添加到ChromeOptions
  11. options.add_argument(f'--load-extension={extension_path}')
  12. # 创建一个ChromeDriverService实例
  13. service = Service(webdriver_path)
  14. # 使用ChromeDriverService和ChromeOptions创建一个WebDriver
  15. driver = webdriver.Chrome(service=service, options=options)
  16. # 使用Chrome DevTools Protocol激活扩展
  17. dev_tools = driver.execute_cdp_cmd('Browser.enable', {})
  18. driver.execute_cdp_cmd('Browser.activateTarget', {'targetId': driver.current_url})
  19. # 现在,您可以使用驱动程序与激活的扩展进行交互
  20. # 例如,您可以打开一个URL并与该页面上的扩展进行交互
  21. 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.

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. from selenium.webdriver.chrome.options import Options
  4. # Path to your Chrome WebDriver executable
  5. webdriver_path = '/path/to/chromedriver'
  6. # Path to the extension CRX file
  7. extension_path = '/path/to/extension.crx'
  8. # Create a ChromeOptions object
  9. options = Options()
  10. # Add the extension path to the ChromeOptions
  11. options.add_argument(f'--load-extension={extension_path}')
  12. # Create a ChromeDriverService instance
  13. service = Service(webdriver_path)
  14. # Create a WebDriver with the ChromeDriverService and ChromeOptions
  15. driver = webdriver.Chrome(service=service, options=options)
  16. # Activate the extension using the Chrome DevTools Protocol
  17. dev_tools = driver.execute_cdp_cmd('Browser.enable', {})
  18. driver.execute_cdp_cmd('Browser.activateTarget', {'targetId': driver.current_url})
  19. # Now you can use the driver to interact with the activated extension
  20. # For example, you can open a URL and interact with the extension on that page
  21. 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.

huangapple
  • 本文由 发表于 2023年5月21日 22:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定