0

I'm trying to automate this basic things with PYTHON:

  • Open browser chrome
  • Open new tab by keyboard ( Ctrl + t )

I only know how to open the browser now and search for a link :

from selenium import webdriver

browser = webdriver.Chrome('location')

browser.get('https://google.com')
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
Salma Madini
  • 15
  • 2
  • 9

2 Answers2

0

There are many ways, the most easy one is to use pyautogui module.

First of install pyautogui then code is as follows:

import pyautogui
from selenium import webdriver
browser=webdriver.Chrome("c:\Webdrivers\chromedriver.exe")  #Location of driver in my case
browser.get('https://google.com/')

# To open a new tab
pyautogui.hotkey('ctrl','t')
Abanish Tiwari
  • 167
  • 1
  • 2
  • 14
0

Please add the below line in your code.

from selenium.webdriver.common.keys import Keys

browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
Surajit Mitra
  • 414
  • 8
  • 15
  • Traceback (most recent call last): File "", line 1, in AttributeError: 'WebDriver' object has no attribute 'send_keys' – Salma Madini Sep 01 '19 at 10:48
  • Try changing the element from 'body' to some other element and try. If still doesn't work, then try this browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') or browser.execute_script('''window.open("http://bings.com","_blank");''') – Surajit Mitra Sep 01 '19 at 11:11