0

I'm trying to launch some browser (Chrome) functions by sending shortcuts I have tried several methods but they all cannot work.

I do this by following these steps

Initialize the browser

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains

options = Options()
options.add_argument("--user-data-dir="+r"path_to_user_data")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())
                      ,options=options
                     )

Through actionchain

ActionChains(driver).key_down(Keys.CONTROL).key_down('T').key_up('T').key_up(Keys.CONTROL).perform

Through send_key

driver.find_element(by=By.XPATH,value="/html/body").send_keys(Keys.CONTROL+"T")

But it doesn't work. This is confused for me. Why it cannot work?

silver
  • 13
  • 3

2 Answers2

0

To Open a blank new tab you can use the below:

driver.execute_script("window.open('');")

Switch to the new window

driver.switch_to.window(driver.window_handles[1])
Akzy
  • 1,817
  • 1
  • 7
  • 19
  • Sorry, maybe my example was a bit misleading. It's not exactly what I wanted. In fact, I have a Chrome extension that I want to invoke by way of a shortcut. Ctrl+t is just an example to test if the shortcut works well – silver May 23 '22 at 14:49
  • I think @sound wave has clarify you in more detail with example. – Akzy May 24 '22 at 10:34
0

It is possible to open a new tab and switch to it with a single command

driver.switch_to.new_window()
sound wave
  • 3,191
  • 3
  • 11
  • 29
  • Sorry, maybe my example was a bit misleading. It's not exactly what I wanted. In fact, I have a Chrome extension that I want to invoke by way of a shortcut. Ctrl+t is just an example of how I can test if the shortcut works – silver May 23 '22 at 14:48
  • @silver I think you can do that with `pyautogui.hotkey('ctrl', 't')`, before you have to install the package `pip install pyautogui` and then `import pyautogui` in the script. Try and let me know – sound wave May 23 '22 at 18:37
  • That's exactly what I'm doing now, but I'm still looking for ways to be able to send shortcuts so that it's easier to work in the background – silver May 24 '22 at 04:19
  • 1
    @silver Webdriver is designed to be used to drive the web-page, rather than to do browser specific actions. See here https://bugs.chromium.org/p/chromedriver/issues/detail?id=581 "This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked" – sound wave May 24 '22 at 05:35
  • @silver good! you can mark the answer as accepted if it esaustively answers your question, thanks – sound wave May 24 '22 at 06:19