Home > Net >  python selenium sendkey doesn't work in chrome
python selenium sendkey doesn't work in chrome

Time:05-24

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?

CodePudding user response:

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])

CodePudding user response:

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

driver.switch_to.new_window()
  • Related