Send keys control + click in Selenium with Python bindings Send keys control + click in Selenium with Python bindings selenium selenium

Send keys control + click in Selenium with Python bindings


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

import timefrom selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.keys import Keysdriver = 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()


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 windowsecond_driver = webdriver.Firefox() #2nd windows 


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 windownewTab.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.