python-selenium and ChromeDriver: Can't open tab to control it - python

I'm unable to open a new tab - or even click on a link and open it on a new tab - with Python 2.7.1, Selenium 2.53.0, ChromeDriver 2.22 and Google Chrome 51.0 on Mac OS X.
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get('https://twitter.com')
driver.find_element_by_id('signin-email').send_keys(Keys.COMMAND + 't')
It opens the URL, but it doesn't open a new tab.
I've also tested opening a new tab with ActionChains, but no success.
The only method to open new tab that works is with JavaScript through execute_script(), but I'm not able to control the new tab in that case. switch_to.window() doesn't work.

You should try as below :-
driver.find_element_by_id('signin-email').send_keys(Keys.COMMAND + Keys.RETURN);
Hope it will help you..:)

You can try to click on the link while holding the ctrl key, this will open the link in new tab
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).perform()
driver.find_element_by_id('signin-email').click()
actions.key_up(Keys.CONTROL).perform()

Related

CTRL + CLick or CTRL + Enter not working its desiable by javascript cant open new tab [duplicate]

There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python.
Perform some tasks in the new tab and then close that tab and return back to the main tab.
Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.
I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer
url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source
py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()
I expect the link in div[data-res-position = '1'] to open in a new tab
As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:
To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/")
link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")
Browser Snapshot:
You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver
Update
To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python
This is trick can be achieve if your locator return a correct href.
Try this first:
href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
print(href)
If you get correct href, you can do:
#handle current tab
first_tab = driver.window_handles[0]
#open new tab with specific url
driver.execute_script("window.open('" +href +"');")
#hadle new tab
second_tab = driver.window_handles[1]
#switch to second tab
driver.switch_to.window(second_tab)
#switch to first tab
driver.switch_to.window(first_tab)
Hope this helps.
Following import:
selenium.webdriver.common.keys import Keys
You have to send key:
py_button.send_keys(Keys.CONTROL + 't')

How can I open a new tab with selenium python?

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)
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')")
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).
You can use pyautogui to doy to open new tab using ctrl+t :
import pyautogui
pyautogui.keyDown('ctrl')
pyautogui.press('t')
pyautogui.keyUp('ctrl')
driver.switch_to.new_window()
From the webdriver docs
You can pass in 'tab' or 'window'. For ChromeDriver, it opens a tab by default.

Why are Selenium Chromedriver shortcuts not working?

I am using chromedriver.exe for Chrome version 80, and chrome version 80.0.3987.149. My selenium is up to date. I'm trying to send shortcuts to the browser in python and nothing is working. I've tried ActionChains, and elem.send_keys(). No shortcuts I've used have worked and I can't figure this out.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import pyautogui
driver = webdriver.Chrome(executable_path=r'path/to/exe')
driver.get('https://www.google.com')
time.sleep(8)
elem = driver.find_element_by_xpath("//body")
elem.send_keys(Keys.CONTROL,'t') #Opens a new tab
This doesn't work! I've tried an ActionChain approach and that doesn't work either. Any help would be awesome.

How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver

There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python.
Perform some tasks in the new tab and then close that tab and return back to the main tab.
Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.
I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer
url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source
py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()
I expect the link in div[data-res-position = '1'] to open in a new tab
As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:
To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/")
link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")
Browser Snapshot:
You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver
Update
To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python
This is trick can be achieve if your locator return a correct href.
Try this first:
href = driver.find_element_by_css_selector("div[data-res-position = '1']").get_attribute("href")
print(href)
If you get correct href, you can do:
#handle current tab
first_tab = driver.window_handles[0]
#open new tab with specific url
driver.execute_script("window.open('" +href +"');")
#hadle new tab
second_tab = driver.window_handles[1]
#switch to second tab
driver.switch_to.window(second_tab)
#switch to first tab
driver.switch_to.window(first_tab)
Hope this helps.
Following import:
selenium.webdriver.common.keys import Keys
You have to send key:
py_button.send_keys(Keys.CONTROL + 't')

Selenium not able to open new Window/Tab

I am currently trying to crawl a website using selenium.
I have a table with elements and I want to click on each element, open the link in a new window/tab, process this window/tab, close it and click on the next element etc. So far I can click on the elements of the table, open the link and process the page.
Unfortunately I can not open the link in a new window or tab.
I can not send keyboard commands to the elements.
I have also tried
action = ActionChains(self.driver)
action.move_to_element(ele)\
.key_down(Keys.SHIFT)\
.click(ele)\
.key_up(Keys.SHIFT)\
.perform()
This somehow does only open the link in the current window (I dont't get a new window handle, still just one).
I would be very grateful for any help.
Edit: Also I am not able to open the link in a new window/tab manually with the browser.
To access the URL e.g. https://www.google.co.in then to click on an element e.g. link with text as Gmail to open the link in a new window/tab you can use action_chains class as per the solution below :
Code Block :
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
mail_link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
ActionChains(driver).key_down(Keys.CONTROL).click(mail_link).key_up(Keys.CONTROL).perform()
Browser Snapshot :
As Gmail is an 'a' tag, first get that a tag like below and perform an actionchain
from selenium.webdriver.common.action_chains import ActionChains
gmailtag = webdriver.find_element_by_xpath('path to gmail') # you can use any other methods to get it.
#Now you can run this to open the gmailtag in a new tab
ActionChains(webdriver).key_down(Keys.CONTROL).click(gmailtag).perform()
# this also worked for me
gmailtag.send_keys(Keys.CONTROL+Keys.ENTER)
It would open Gmail in a new tab. It worked for me.

Categories

Resources