2

I'm trying to make a program that opens multiple websites, but I can't get it to press control-t. I've tried multiple solutions, but I can't find one that works. When I do the keydown method, I get an error that says

webdriver has no attribute key_down

and when I try send_keys(Keys.CONTROL + 't') it doesn't raise any errors, or do anything. How can I open a new tab?

Here's my try :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://youtube.com")
search = driver.find_element_by_id("search")
#search.keydown(Keys.CONTROL)

#Webelement.key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()

search.send_keys(Keys.CONTROL+'t')
time.sleep(10)
Xitiz
  • 3,548
  • 1
  • 7
  • 30
GOODTOAD
  • 45
  • 5

3 Answers3

3

You can do it as

from selenium import webdriver

driver.get("https://www.youtube.com")
search = driver.find_element_by_id("search")

driver.execute_script("window.open('https://www.google.com')")
Xitiz
  • 3,548
  • 1
  • 7
  • 30
YaDav MaNish
  • 1,202
  • 2
  • 11
  • 19
  • Does this really solved your problem? @GOODTOAD I don't think this is really complete answer. I am not asking you to accept my answer but I believe my answer is more clear then this! – Xitiz Aug 05 '21 at 05:57
1

Here's what you can try :

from selenium import webdriver

webBrowser = webdriver.<>()
  
# This is first tab
webBrowser.get('<>')

# Second tab
webBrowser.execute_script("window.open('about:blank','secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('<>')

# Third tab
webBrowser.execute_script("window.open('about:blank','thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('<>')

You can learn more about it from Python – Opening multiple tabs using Selenium

Additionally I think you also have you look here.

However, if you want to run something on multiple windows, then I recommend having multiple instances of webdriver. It is much easier to manage, and is supported (There are workarounds on opening a new tab/window, such as pressing a hotkey that opens a new window, but they aren't supported).

Xitiz
  • 3,548
  • 1
  • 7
  • 30
1

You can use pyautogui to doy to open new tab using ctrl+t :

import pyautogui

pyautogui.keyDown('ctrl')
pyautogui.press('t')
pyautogui.keyUp('ctrl')
Xitiz
  • 3,548
  • 1
  • 7
  • 30
farhan jatt
  • 432
  • 3
  • 19