How to use Control+Click Selenium Webdriver to open a link embedded in a web element, the link is in the main tab, in a new tab in the same window


Devas

There is a link embedded in the web element of the main tab and I want to open the link in a new tab in the same window using Selenium Webdriver and python. Perform some tasks in a new tab, then close the tab and return to the main tab. We'll manually open the link in a new tab by manually right-clicking the link and selecting "Open in New Tab".

I am new to selenium. I am using Selenium and BeautifulSoup for web scraping. I just know how to click the link. I have read many posts but I can't find the correct 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 want the link div[data-res-position = '1']to open in a new tab

DebanjanB

Since a link is embedded in the web element of 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:

In order to demonstrate the URL's workflow https://www.google.com/open the parent tab , then open in new tabfunctionalty by implementing the ActionChainsmethod key_down(), click()as well as the key_up()method.

  • 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 will need to replace with (By.LINK_TEXT, "Gmail")the desired locator, e.g.("div[data-res-position = '1']")

  • Browser snapshot:

tab_actions

You can find a related Java - based solution in " Open New Tab" using the Ctrl+click key combination in Selenium Webdriver .


Updates

To shift the focus of Selenium to the newly opened tab, you can find a detailed discussion in the new tab Selenium + Python of "Opening the Web" .

Related


Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

Javascript - open link in "new tab" (SAME WINDOW)

to I realize there are already several questions on this topic, but they all seem pretty old... just trying to get an up-to-date answer: Still the standard way to open a new tab in the same browser window: window.open('url', '_blank'); window.focus(); ??? Als

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing happens. PS I am using pyQt5.6 I've tried it on Linux centOs,

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing happens. PS I am using pyQt5.6 I've tried it on Linux centOs,

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing happens. PS I am using pyQt5.6 I've tried it on Linux centOs,

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing is done. PS I am using pyQt5.6 I've tried it on Linux centOs,

Window cannot open new window or tab on external URL link click

Rajish Kapoor I need to open a URL using pyQt5. The page has several links that open new windows. pyQt5 opens a window for the URL, but after clicking the link that should open a new window, nothing is done. PS I am using pyQt5.6 I've tried it on Linux centOs,

JavaScript: How to open link in new tab without window.open()?

Brandon I have a href that I need to open in a new tab and can't use anchor tags because it's from a webgl context. Here's how to accomplish this task: window.open(href, "_blank"); However, the big caveat that comes with it is that the new tab shares a runtim

JavaScript: How to open link in new tab without window.open()?

Brandon I have a href that I need to open in a new tab and can't use anchor tags because it's from a webgl context. Here's how to accomplish this task: window.open(href, "_blank"); However, the big caveat that comes with it is that the new tab shares a runtim

JavaScript: How to open link in new tab without window.open()?

Brandon I have a href that I need to open in a new tab and can't use anchor tags because it's from a webgl context. Here's how to accomplish this task: window.open(href, "_blank"); However, the big caveat that comes with it is that the new tab shares a runtim

JavaScript: How to open link in new tab without window.open()?

Brandon I have a href that I need to open in a new tab and can't use anchor tags because it's from a webgl context. Here's how to accomplish this task: window.open(href, "_blank"); However, the big caveat that comes with it is that the new tab shares a runtim