Send keys control + click in Selenium with Python bindings
Asked Answered
M

6

31

I need to open link in new tab using Selenium.

So is it possible to perform ctrl+click on element in Selenium to open it in new tab?

Marlonmarlow answered 5/1, 2015 at 8:27 Comment(1)
This post can help.Blowsy
G
52

Use an ActionChain with key_down to press the control key, and key_up to release it:

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

driver = webdriver.Chrome()

driver.get('http://google.com')
element = driver.find_element_by_link_text('About')

ActionChains(driver) \
    .key_down(Keys.CONTROL) \
    .click(element) \
    .key_up(Keys.CONTROL) \
    .perform()

time.sleep(10) # Pause to allow you to inspect the browser.

driver.quit()
Gunar answered 5/1, 2015 at 11:38 Comment(0)
M
12

Two possible solutions:

opening a new tab

self.driver = webdriver.Firefox()
self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 

this is the solution for MAC OSX. In other cases you can use the standard Keys.CONTROL + 't'

opening a new webdriver

driver = webdriver.Firefox() #1st window
second_driver = webdriver.Firefox() #2nd windows 
Mccleary answered 5/1, 2015 at 10:22 Comment(3)
Nothing happens after self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') in ChromeCholesterol
This answer, as stated, was tested on Firefox in 2015 not on Chrome . It is possible that new drivers versions require an update. About Chromedriver compatibilities you can check sites.google.com/a/chromium.org/chromedriver/downloads and seleniumhq.org/about/platforms.jspMccleary
I installed current driver version :(Cholesterol
P
0

Below is what i have tried for Selenium WebDriver with Java binding and its working for me. If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting 'Open in new Tab' option. Below is the implementation in Selenium web-driver with Java binding.

Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));

//Open the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.

driver.switchTo().window(windowName);

You can keep track of window-names which will help you to easily navigate between tabs.

Pocketknife answered 5/1, 2015 at 9:53 Comment(0)
B
0

By importing Keys Class, we can open page in new tab or new window with CONTROL or SHIFT and ENTER these keys: driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.CONTROL,Keys.ENTER)

or

driver.find_element_by_xpath('//input[@name="login"]').send_keys(Keys.SHIFT,Keys.ENTER)

Bruise answered 4/4, 2019 at 17:48 Comment(0)
D
0

For python, a good solution would be driver.find_element_by_css_selector('<enter css selector here>').send_keys(Keys.CONTROL, Keys.RETURN)

After that, you should change windows so selenium could function in the new window

window_before = driver.window_handles[0]    
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)

After using the window is useful to close it or go back to the original:

Close tab: driver.close()

Back to the original: driver.switch_to_window(window_before)

Also try driver.switch_to.window() if your Selenium version does not support the other one

Darya answered 9/6, 2020 at 15:14 Comment(0)
L
-4

Following is working for me to open link in new tab :

   String link = Keys.chord(Keys.CONTROL,Keys.ENTER); 
   driver.findElement(By.linkText("yourlinktext")).sendKeys(link);

Above code is in java. you can convert to python easily I assume.

Please ask if have any query.

Libra answered 5/1, 2015 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.