Send keys control + click in Selenium with Python bindings

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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.

Method 4

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)

Method 5

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

Method 6

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x